0

When using task scheduler, if you run a batch program, when ending the script, it only closes the batch file, and not the program spawned by the batch file.

Is there a way to close all programs opened by that batch session when it gets a terminate command?

e.g.

echo started: %date% %time% >> log.txt
calc
timeout 5
echo ended: %date% %time% 
taskkill /im win32calc.exe

Is there a way in batch to run the taskkill command (or another way to close all spawned processes) when it tries to get force closed.

In python, this would be done with a try/finally, like this: try: ... finally: killtask('win32calc') where the finally always runs no matter what.

Or with open 'calc.exe': # run some code

A H
  • 2,164
  • 1
  • 21
  • 36
  • 1
    Try: `taskkill /im calc.exe` I just tested it and it works OK... – Aacini Jul 26 '18 at 14:02
  • The `taskkill` command works just fine. I want a way for it to run WHEN someone closes the batch file (i.e. by pressing the x button, or end task). e.g., if the `timeout` gets to 3 seconds, and we close the batch file, calc.exe stays open. How can I close the calc file , because the batch file is closed. – A H Jul 26 '18 at 14:18
  • Are you saying that whenever you close the host of any running python script, that python script and everything which was opened/spawned from any command within it will be closed too? – Compo Jul 26 '18 at 16:00
  • @Compo That is exactly the behaviour I want. – A H Jul 26 '18 at 16:06
  • I know what you want! I asked you if this happens whenever you close the host of a running python script, because that's what you've implied. If it does, 1. Why can't you use python? 2. Prove it! – Compo Jul 26 '18 at 16:26
  • @Compo, I can use Python, however not everyone on my team knows python, so it would be nice to have a Windows way of doing it. Batch files are also easier to work with (no need for python environments etc.). – A H Jul 27 '18 at 08:03
  • 1
    What do you mean by `get force closed` ? There are variety of ways for a process to get closed. If you mean closing by `TerminateProcess` function which is apparently what `TASKKILL` does by `/f` switch, then no process, python or whatever, can have control over it and know in advance that it is getting terminated to able to handle the situation; If you mean closing by clicking on the `X` button on the console window of the host process, then you need to handle the `CTRL+CLOSE` signal which is simply not possible with a batch script. – sst Jul 27 '18 at 09:02
  • 1
    It can be done in some dirty ways using `WMIC`, The main idea is to use a watchdog process (similar to what *Aacini* showed in his answer), which must know in advance the PID of the main process to periodically check if it has terminated, and after that attempts to terminate the spawned child processes. The main Process needs obtain its own PID (See [getting-process-id-of-exe-running-in-bat-file](https://stackoverflow.com/a/27595129/4561332)) and tell it to watchdog and also the PID of each process it spawns and record them in file to be later used by the watchdog to terminate them. – sst Jul 27 '18 at 10:18

1 Answers1

2

I think the behavior you want can be implemented via a modification of this answer, that is:

rem Start the process that will kill the calc.exe program after 5 seconds
start "WaitingToKill" cmd /C timeout /t 5 ^& taskkill /im calc.exe /f

rem Run the calc.exe program and wait for it to terminate
calc.exe

rem If calc.exe ends before 5 seconds, kill the killer process and terminate
taskkill /fi "WINDOWTITLE eq WaitingToKill" /f
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • That is a good answer, however our scripts/programs may take 10 minutes to run, or 10 hours. And when the batch file is terminated, and I want to re-run the process, it may hang because the process is still open (and then i'd have to remember to terminate the process. Doesn't work when everyone else in the team doesn't know that ending the batch task doesn't end the processes it spawned :p). – A H Jul 27 '18 at 08:02