4

Is there a way to kill a process tree using the terminal?

By killing a process tree, I mean killing all descendants of a process along with the process it self.

I tagged this question htop because I am using that utility a lot these days and it would be cool if there was an easy way to kill a process tree inside htop (something better than just selecting all the processes in the subtree).

5 Answers 5

7

There has been a feature in htop for this for a long time, but it was missing from the manual until 2018: press the c key to select the currently highlighted process as well as all of its children. Then operations that apply to selected processes (such a killing using the k key) apply to the process and its children.

This isn't atomic, though. If new children are spawned between the moment you press c and the moment the kernel has killed all the processes, they won't be killed and will most likely end up being reattached to the init process. This is because htop is looping on the selected processes and killing them one by one.

I've proposed a patch to add an atomic group-killing feature to htop a while ago, but the implementation wasn't consensual enough for it to be merged. With it, it is possible to kill a whole process group atomically from htop. That's not exactly the same as killing a whole tree, but AFAIK, the kernel doesn't provide any way to kill an arbitrary tree atomically.

5

This should help you!

pkill -TERM -P `PID`
4
  • where PID is the process ID of the parent process?
    – a06e
    Commented Oct 30, 2014 at 19:27
  • @becko Yes. PID is the parent's Process ID
    – BDRSuite
    Commented Oct 30, 2014 at 19:32
  • This is must be the correct answer! What is TERM mean?
    – GoingMyWay
    Commented Aug 8, 2017 at 6:38
  • 3
    I think this would kill only direct children and not the entire tree leaving grandchildren orphaned.
    – Sogartar
    Commented Apr 16, 2019 at 19:23
1

Using htop, you can use F5 to show the process tree's.

If you select the process at the top of the tree you want kill, then press F9 followed by Enter it will close the process and the entire process tree in one go.

In the screen shot below this action would cause Chrome and all sub process to be closed.

enter image description here

2
  • 1
    I've done this, but sometimes what happens is that the descendant process becomes "parentless", i.e., its PPID is assigned to 0. This has never happened to you?
    – a06e
    Commented Oct 30, 2014 at 19:37
  • The statement “it will close the process and the entire process tree in one go” is incorrect. If you send a TERM signal (not KILL) to the parent process, it may handle it gracefully, terminating its own children. If children receive a HUP signal when their parent is terminated, they might terminate by themselves. But the general rule is that children get reattached to the init process (PID 0). Never assume killing a process is going to kill its children as well.
    – Arkanosis
    Commented Aug 7, 2019 at 9:24
0

Yes, this can be done by using kill, i am not sure about htop. For example, if the PGID of the processes 65,78 and 90 is 35, then you can kill them all (forcefully) by the following command:

sudo kill -9 -35

i.e.

sudo kill -9 -<PGID>

2
  • This kills the entire subtree? I mean, what if 35 is the parent of 65 and 78, but 78 is the parent of 90?
    – a06e
    Commented Oct 30, 2014 at 19:26
  • Only if its in the same process group. You can use ps -eo pid,ppid,pgid,args to have a better idea. The thing is basically the PPID becomes the PGID of a group of processes started afterwards the Parent process.
    – heemayl
    Commented Oct 30, 2014 at 19:37
-1

I tried this and it worked for me

  1. Get the PID process id by top or Htop (with Htop tape F4 write-the-name-of-the-process then F5 to get tree)

  2. use this command it worked for me

    kill -TERM pid
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .