I am executing the following command to stop entire process tree(i.e parent and all its child/grand-child processes) :
pkill -9 -s {sessionID}To get sessionID I am using following command:
ps -o pid,sess -u <username> | grep <pid> | awk '{print $2}'Sometimes pkill command kills entire process tree, but sometimes it doesn't work. Some of the child process still be running in background. Why is that so? Is there anything wrong in my logic? Is there any boundary condition missing? Are there any situations where pkill command doesn't kill child processes?
11 Answer
A process' children need not necessarily share its session ID. If that were the case, every process would have session ID 1. You can also try using process group IDs, but that too has the same limitation.
Walking through the process tree has the limitation that double-forked processes aren't part of the tree anymore, but might be your best bet. Examples of this method:
- What's the best way to send a signal to all members of a process group?
- Kill all descendant processes