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?

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.

  • Mate, as I wrote, using the CMD /C syntax will not allow to terminate properly the task after a given duration – Riccardo Sep 17 '13 at 14:04
  • @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 Sep 17 '13 at 14:41
  • 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 Sep 17 '13 at 15:11
  • 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 Sep 17 '13 at 15:15
  • @Riccardo - Fair enough. I understand your problem now. – dbenham Sep 17 '13 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.