Search code examples
pythonsubprocesspopenkillkill-process

python terminate/kill subprocess group


I have a few python scripts who are opening themselves in cascade by subprocess.Popen().

(I call script1.py who make a popen of script2.py who makes popen of script3.py, etc)

Is there any way to terminate/kill all subprocesses of script1.py from the script1.py PID.

os.killpg() doesn't work.

Thanks for your help.


Solution

  • By changing the gid at the beginning of the execution of script2.py, the sub sequent processes belongs to script2 gid. So calling killpg() from script1.py with script2's pid does it well.