Scheduled Task and environment variables WITHOUT CMD /C

1

I need to run a task within Windows 2003 Task Scheduler however the command needs to access environment variables:

vlc.exe http://62.196.56.141:8080 --sout=#std{access=file,mux=raw,dst="C:\temp\streaming\%Date:~6,4%-%Date:~3,2%-%Date:~0,2%-RSMorning.mp3"}

While the above command will work when issued from a batch file or a command prompt, it will not expand correctly the variables when run from the Task Scheduler.

Someone would argue that this can be achieved using the CMD /C prefix, allowing the command to be launched within a command prompt, however as the task has to be forcibly terminated after a while, cmd.exe would be terminated in place of the invoked command (in this case, vlc.exe).

Any hint how to run the command without using a batch file or the command prompt, while inheriting correctly environment variables?

Riccardo

Posted 2013-09-17T13:35:22.350

Reputation: 425

Are you sure you don't run into this problem?

– nixda – 2013-09-18T12:58:51.743

Answers

0

Try this

cmd /c start "" vlc.exe http://62.196.56.141:8080 --sout=#std{access=file,mux=raw,dst="C:\temp\streaming\%Date:~6,4%-%Date:~3,2%-%Date:~0,2%-RSMorning.mp3"}

You can kill vlc with something like

taskkill /fi "imagename eq vlc.exe"

EDIT - Now that I fully understand your dilemma:

I'm pretty sure there is no simple solution that gives you access to the environment variables and also lets task scheduler kill vlc after time elapses. Your program knows nothing about environment variable expansion, so that must occur before vlc.exe is called. That is the job of cmd.exe. Unfortunately, Windows does not have true child processes, so if you launch vlc.exe via cmd.exe, then when you task scheduler kills cmd.exe, the vlc.exe process remains.

I believe you will have to write a script that launches vlc.exe, remembering the PID. The script would then wait your prescribed amount of time, and then kill vlc. Task scheduler can launch your script.

I believe you can do what you want with a batch script on Windows 2003, depending on which tools (commands) are available on that version of Windows.

TIMEOUT can be used to introduce a delay. For example, to wait 1 minute:

timeout 60 /nobreak >nul

WMIC can be used to launch a process and return the PID. See Windows CMD Shell» Start a process and get its PID. I think the tricky part will be passing the quoted arguments, but it can be done. Read the entire thread.

dbenham

Posted 2013-09-17T13:35:22.350

Reputation: 9 212

Mate, as I wrote, using the CMD /C syntax will not allow to terminate properly the task after a given duration – Riccardo – 2013-09-17T14:04:46.857

@Riccardo - The CMD process will already be long gone. It closes as soon as START has launched vlc. What is to stop you from killing vlc? It will be its own process. – dbenham – 2013-09-17T14:41:53.447

Task scheduler can optionally terminate the scheduled task after a given duration. Now, as the scheduled task is cmd.exe (with vlc.exe as a parameter), it will not be able to terminate vlc.exe when the duration time has ended – Riccardo – 2013-09-17T15:11:01.590

There will be multiple vlc.exe instances running at the same time! I would need the proper PID to properly kill the proper process.... – Riccardo – 2013-09-17T15:15:01.890

@Riccardo - Fair enough. I understand your problem now. – dbenham – 2013-09-17T20:12:11.373

as far as you are concerned, do you know why a process started from the scheduler cannot access environment variables? Maybe because there's no preprocessor that will handle them? – Riccardo – 2013-09-18T07:01:55.077

@Riccardo - Yes. See updated answer – dbenham – 2013-09-18T12:34:13.250