I am calling a shell script fom within Python, that spawns multiple child processes. I want to terminate that process and all of its children, if it did not finish after two minutes.
Is there any way I can do that with subprocess.run or do I have to go back to using Popen? Since run is blocking, I am not able to save the pid somewhere to kill the children in an extra command. A short code example:
try: subprocess.run(["my_shell_script"], stderr=subprocess.STDOUT, timeout=120) except subprocess.TimeoutExpired: print("Timeout during execution")
popenis preferred. Note that killing children of a child is not necessarly possible (depends onmy_shell_script). They can be detached.