I have a python script that runs forever but I'd like to restart every day just in case it gets into a bad state, gets shut down, etc. I'm scheduling it daily with task scheduler, which is fine.
The problem is that my script spawns other processes, and those child processes don't get killed when the task is stopped (ex: when I manually end it, or a new instance of the task runs). The main script is killed, but the child processes are not. I have to kill them manually through task manager. If I run my main script on the command line, instead of through task scheduler, closing the console kills all child processes, as expected.
I tried running the task with and without "highest privileges", "if the task does not end when requested, force it to stop", "if the task is already running, stop the existing instance", etc. I've tried running taskkill as the first action of the task. I've tried everything I can think of. I'm running the task as myself, and I'm administrator.
The problem seems to be that the child processes spawned by tasks are access protected. I've tried taskkill, pskill, python scripts like this:
import psutil for process in psutil.process_iter(): cmdline = process.cmdline() if "myscript.py" in cmdline: process.terminate()
I always get an access denied error. Example from taskkill:
c:\>taskkill /f /t /im python.exe ERROR: The process with PID 14436 (child process of PID 7928) could not be terminated. Reason: Access is denied. ERROR: The process with PID 7928 (child process of PID 14324) could not be terminated. Reason: Access is denied. ...
Hell, I even tried having my main python script kill its own child process when a termination event comes in, but I don't even get that event with task scheduler, with or without force kill! Again, on a normal command line run, I get termination events as expected. But thwarted by task scheduler once again.
This is also very easy to reproduce with a one line batch file that runs a python script. If task scheduler runs the batch file, and you end the task, the python script will not be killed when the task is ended.
Any idea why child processes spawned by task scheduler tasks are not killed and how to work around it?