How can processes detach when I try to kill them?

I have an application on Linux where I have to kill a whole process tree from within an application. The process tree looks like:

app
└── parent ├── child1 ├── child2 ├── child3 ├── child4 └── child5

Now I have problems when killing the process tree by killing parent: It often happens that several subprocesses are not being killed when I try to kill the parent process. After the kill signal the child processes appear "detached" as these are in the process tree not below the original application app anymore. How can that happen?

2 Answers

Reliably killing a process tree is not a simple matter, there are a few gotchas, like:

  • killing a parent doesn't typically kill its children
  • killing by process group usually fails if the killer process is in the same process group because the killer is itself killed as well, typically before finishing its job
  • identifying all descendants of a process is not trivial

You may want to got through my answers to these more or less related Q&As for details:

1

Generally in POSIX systems it is not true, that killing parent process also kills it's child.

Especially if you use kill -KILL, after killing parent process child process became orphan processes.

You can kill whole process group or search processes by parent pid before killing parent. In shell it can be done using:

ps --ppid <parent_pid>

In python I would suggest using psutil library, especially Process.children method.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like