1

I need to run two processes in parallel from a single batch file, which must be controlled from the Task Scheduler in Windows.

Following this answer https://stackoverflow.com/a/49051876/3834178 I created this batch file:

(
   start "task1" cmd /C "command1"
   start "task2" cmd /C "command2"
) | pause

and I created a Task in the Task Scheduler which runs that batch file.

Starting the Task, everything works: command1 and command2 are executed in parallel and run fine. The Task Scheduler shows "running".

However, if I try to stop the Task in the Task Scheduler, it shows "stopped" but command1 and command2 keep running, I can clearly see them in the Task Manager, and I have to manually stop them by killing them.

Question: how to improve the batch file so that it responds to the "stop" from Task Scheduler?

EDIT: some clarifications.

  • command1 and command2 start web servers and wait forever, i.e. are never ending programs unless stopped
  • pause is real: not sure what it does here, but comes from the other question I linked above; afaik, it should wait for any output coming from the parentheses which never arrives by definition (start suppress any output) and therefore lets the whole script to be running endlessy
  • the Task in Task Scheduler has no special configuration: it is set to run that batch on demand (no triggers) and anything else. I expect to start it with "play" and to stop it with "stop".
edoedoedo
  • 1,469
  • 2
  • 21
  • 32
  • 1
    Why are you using `cmd /C` to start one or more web servers? Also, if your servers are never ending, why are you waiting? _There's no purpose in the parentheses, or the pipe: 1. `@Command_To_Start_Server1`, 2. `@Command_To_Start_Server2`._ – Compo May 28 '21 at 16:35
  • Also, I'm a little confused about your "play" and "stop" expectations. Are you expecting that stopping the batch file will magically stop everything which was started within it? If you want to stop your servers then you need to research which command or commands can do that for you. BTW, `start` does not "suppress any output". Please open a Command Prompt window, type `start /?`, and press the `[ENTER]` key, to learn more about it. – Compo May 28 '21 at 16:49
  • `(command1 & command2)|pause` will wait for both `command1` and `command2` to end before continuing (verify with `(start timeout 2 & start timeout 5)|pause`). You said, they won't end, so this construct makes no sense here. – Stephan May 29 '21 at 08:40
  • Thank you @Compo and @Stephan, so the batch I'm using is completely wrong, I'm sorry about it. My expectation about "play" and "stop" would be: PLAY: execute the two web servers (`command1` and `command2`) in parallel, and reflect the fact that both are running (i.e. not crashed) in the Task status "running"; STOP: terminate the web servers processes and reflect the fact that both are died in the Task status "stopped". Is that achievable? – edoedoedo Jun 03 '21 at 09:39

0 Answers0