I have a python process, which spawns a git (git clone) child process, which in turn spawns several child processes itself. Here, I want to kill all processes in the process tree rooted by git, without killing the python process.
Possible solutions with caveats:
- Traverse the child process tree of
pythonusing/proc/<pid>recursively.
In this case, how can I be sure that any process in this process hierarchy hasn't spawned a child process before it being killed, in which case the child becomes an orphan (race condition)?
- Kill the process group of the
pythonprocess, which includesgitand every other descendant.
In this case, python process is killed too, which I don't want to happen.
Is there any way to reliably kill all processes in the process hierarchy rooted by the python process, but not the python process itself?
kill -pgidwill also kill your python (you can just catch the signal), but because it also kill other sibling or parent processes of your python (as any script it was called from, or other processes from the same pipeline). Generally, if you have to care too much about process management and you're not writing the next systemd, it's probably because you're overcomplicating yourself.