Link to home
Start Free TrialLog in
Avatar of laberet

asked on

scheduled task to run batch script stays open

Environment = Windows Server 2008 R2
Scheduled task that runs as the system account
Target is a batch file example.bat
When the task scheduler runs the scheduled task it invokes cmd.exe with the /K switch that leaves the command line open and all child processes that are spawned by example.bat open.

Why is it using the /K switch?  How can I get the task scheduler to spawn the task with the /C switch so it doesn’t leave several orphaned and open processes each time it runs?
Avatar of neilpage99
Flag of United States of America image

If you're using Task Scheduler to run a batch file, why not simply invoke the batch file as a single "commend" - i.e. without the explicit use of the "cmd.exe" command?

In the properties of the task itself, you've selected the action "Start a program" - yes?

And in the "Program/script" box, can you simply supply the full patch to the batch file and leave it at that?
Avatar of laberet


We started with just the file name of the "example.bat" in task scheduler.  We can see in process explorer that the scheduler is running it via cmd.exe /K.  We tried adding the "option" of /C but task scheduler just ignored that and used the same cmd.exe /K

The batch scripts spawn several sub batch scripts.  The process runs but it leaves a half dozen open processes each time which is not ok.

We did not see this behavior prior to allowing task scheduler to run these.  not sure if its task scheduler, the default properties of the association of .bat and cmd.exe or related to it running under the system account.
I'm fairly sure that if you run a .bat script from the task scheduler, the scheduler itself is only ever going to know about that exact .bat. It's not like a windows service that has dependencies or a process that has child processes. If your .bat script calls other .bat scripts or executables, those other scripts/exe have a life of their own - so if they don't run and exit properly, you'll end up with them running headless in the background using up resources.
Maybe in your originally called .bat script, at the end of it you can write code to see if your other spawned processes have completed yet, and if not, kill them. I don't think you'll be able to change the behavior of task scheduler - it's only ever concerned about the initially called batch script.
Have you tried making your scripts run sliently?

Although it may/may not resolve a process that doesn't complete.
Avatar of Lionel MM
Can you show us the contents of example.bat?
Avatar of laberet

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of laberet


Other suggestions did not solve the problem.