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?
Microsoft Legacy OSMicrosoft Server OSMicrosoft DOS

Avatar of undefined
Last Comment

8/22/2022 - Mon

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?

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

Have you tried making your scripts run sliently?

Although it may/may not resolve a process that doesn't complete.
Lionel MM

Can you show us the contents of example.bat?

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Other suggestions did not solve the problem.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.