1

I want to kill a chain of process e.g.

PID  PPID
100  XXX
101  100
102  101

When I use pkill -P 100 PID 100 and 101 die as expected, but 102 is given init as it's PPID.

How can I force pkill to keep killing chained processes past a process's immediate descendants?

2

pkill is doing exactly what you told it to do: it killed the processes whose parent is 100. Not processes whose parent's parent is 100.

Neither Linux's pkill nor FreeBSD's has an option to traverse the process tree. You can call pstree -l and parse its output.

Keep in mind that if A forks B, B forks C and then B dies, there's no parent-child relation left that can connect A and C.

There may be a way to kill all these processes, but beware that it might overmatch.

If you pass a negative process ID to kill, that kills the whole process group. This is atomic, so it works even if one of the processes forks just as you're running kill. This only kills the processes that haven't placed themselves in their own group, and it also kills the parent process and others if they're in the same process group. Run ps -o pgid … to see the process group ID of processes.

If all the processes you want to kill have a particular file open, you can use fuser -k /some/file to kill all the processes that have this file open.

Another option on Linux is to run the processes in their own PID namespace. You can then kill the whole namespace with by killing the pseudo-PGID -1 from within the namespace.

  • I see. I was afraid that was the case. Any reason why what I want is not implemented? Is it just because killing by process group is just a "better" way? – noobcoder Jun 22 '16 at 23:57
  • @noobcoder I think because it's unreliable: if a process in the middle dies, the connection is lost. In many concrete scenarios, the PGID is exactly what's needed, and that's implemented in the kernel. In fact, the PGID is what you ask for, except that a process has a way to opt out of the tree. Linux provides another way with namespaces, this one doesn't have an opt-out. – Gilles Jun 23 '16 at 0:01
  • Thanks for this. Killing by process group and it works perfectly now. – noobcoder Jun 23 '16 at 1:40

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.