Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

closed as off topic by sysadmin1138 Jul 10 '11 at 23:56

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers 4

up vote 9 down vote accepted

This SO question covers this.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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"

share|improve this answer

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