How do you kill a process tree in linux? [closed]
Sometimes, sending a SIGTERM to a process will cause it to send SIGTERM to all its child processes. However, sometimes this doesn't work.
Is there a command or a utility that will allow me to kill a process and all its child processes at the same time? I usually resort to manually collecting all the pids into one kill command, but it feels stupid.
This SO question asks how to do this with perl, but anything that gets the job done would be great.
This SO question covers this.
Assuming that the processes share a session identifier (which they should unless they've explicitly called setsid(), you can kill them by session using pkill:
user@machine:~> ps -o pid,sess,cmd -U user
PID SESS CMD
12804 12804 -bash
12916 12804 ps -o pid,sess,cmd -U user
user@machine:~> sh
sh-3.00$ sh
sh-3.00$ sh
sh-3.00$ sh
sh-3.00$ sh
sh-3.00$ sh
sh-3.00$ ps -o pid,sess,cmd -U user
PID SESS CMD
12804 12804 -bash
12920 12804 sh
12921 12804 sh
12922 12804 sh
12924 12804 sh
12926 12804 sh
12928 12804 sh
12937 12804 ps -o pid,sess,cmd -U user
sh-3.00$
If from another terminal I do:
pkill -9 -s 12804
Then everything dies in one fell swoop.
You can similarly kill by process group, though this tends to be more useful for many children that are all one level below a parent, not a chain of associated processes.
Usually, a responding client should receive a sigterm and off itself, thereby terminating all its forked child processes. If, on the other hand, the client is not responding, a normal termination signal will probably evoke no response.
So you then proceed to send a SIGKILL, which tells the system itself to Make That Process Stop, Now - which it can do most of the times, except when the process is in a state of ininterruptible sleep, like waiting for some I/O input or similar.
In that case, even the SIGKILL will end up doing nothing to the process; the child process will probably in a similar state of retardation.
There is a border case where the parent process might terminate just nicely, but the child process is not killable. Then you will end up with a zombie process that cannot be cleaned up since its father is gone, but it is not responding to anything. The only way to get rid of those is a reboot; in most cases, though, they are quite harmless, as the only thing they do is take up a PID while whatever socket they tried to use has withered away already.
If I want to kill the process tree of Firefox, then is it possible to kill it by the above command "pkill -9 -s firefox"