1

How can one kill a process group safely, and force kill if needed?

The goal is to do a kill with SIGTERM (-15) then if the apps in the tree do not die after a timeout (e.g. 60 seconds) do a SIGKILL (-9).

We have a scenario with parent process and a child process, sharing a pgid, E.g

Parent Process pid=123, pgid=123
|- Child Process pid=456, pgid=123

Currently we are sending the SIGTERM (-15) to the process group, then if the parent process is dead, then assume success.

However, sometimes we found that the parent process dies, and the child process does not die.

So this boils down to two questions:

  1. Why does the parent pid die if the child does not die (is this possible, or are my assumptions wrong)?

  2. Would checking to see if there are any processes in the process group be the best way to make sure all the processes in the tree are dead before sending the kill -9 to the process group?

  • If the parent dies, what is the status of the children, are they listed as zombie processes (in top/ps) ? Then they simply cannot signal their shutdown properly to the parent. IMHO process group kills need the cooperation of the children, i.e. also reacting properly to SIG 15. Or parent should shutdown the children properly. en.wikipedia.org/wiki/Zombie_process. – AndreasM Aug 3 '17 at 9:29
0
  1. The parent has to explicitly wait() for its children if it cares about their termination state. By default orphaned children are transferred to init (pid1) and not terminated.
  2. Unfortunately, when they are adopted by init, their pgid changes to their own pid (orphan becomes its own session and pg leader).

If you want to be sure, you'd have to make a list of all the PIDs in the pg, then poll to make sure they died. The process group itself is disbanded when the leader dies so you can't monitor processes in that pgid.

If you really, really want to be sure (because PIDs can be reused on very busy systems), you'd need to do a depth-first process tree traversal parent-to-child and kill the children leaf nodes, waiting for each to terminate before killing the next level, but you may run afoul of parents respawning their dead children. The script in this answer on SO handles this edge case.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.