Kill process tree using terminal?

Solution 1:

This should help you!

pkill -TERM -P `PID`

Solution 2:

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.