Go Back

How do you setup the environment variable when using the windows task scheduler

I can create a windows task to run once - but when the task runs it does not know about the environment variables.  
This is true when running under the system account, my account, or my admin account.

Any suggestions

schtasks /create /f /sc ONCE /st 07:48:58 /tn "\Synergy Tasks\tasknamegoeshere" /ru system /tr "'C:\PROGRAM FILES\SYNERGEX\SYNERGYDE\DBL\bin\dbs.exe' 'C:\FLEXcon2\EXE\CUSDPL.dbr'"

2 Answers
0   | Posted by Barry Gershman to Visual Studio Integration on 4/24/2017, 2:30 PM
Beth Ives
Hi Barry,
I have read a few threads on this subject using Google and I saw several statements that suggest the problem could be resolved with a reboot.  I included a couple of threads I read.

https://www.pcreview.co.uk/threads/scheduled-task-and-environment-variables.1496361
https://superuser.com/questions/331077/accessing-environment-variables-in-a-scheduled-task


 

4/24/2017, 3:35 PM   0  
Ace Olszowka
Beth is correct.

If you are interested in why this is the case it is because Task Scheduler runs as a child of Services Manager (SCM aka services.exe); because of the order in which it starts and how environment variables are inherited in Windows often times a reboot is required to have services detect changes in environment variables. You could attempt to restart the Task Scheduler Service (sc stop schedule/sc start schedule) to see if that picks up your variable but I would suspect not.

Here's a good explanation of the Windows Startup Sequence (as of Windows 7) Remember this is an implementation detail and is subject to change at any point:
http://stackoverflow.com/questions/13685960/whats-the-order-of-windows-startup

As an aside this is one of the many reasons why I personally would strongly advise against utilization of Environment variables and instead encourage a solution that relies on either a configuration file or Registry Settings.

4/24/2017, 7:39 PM   0  
Please log in to comment or answer this question.