Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

htop allows me to nicely see trees of processes within the shell. I can kill processes by pressing F9 (KILL) and then selecting which signal (e.g. 15 SIGTERM) I want to send to a job to kill.

However, this only allows me to kill one process at a time. Is there a way to kill a full tree of processes using htop?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

From man htop:

INTERACTIVE COMMANDS

Space Tag or untag a process. Commands that can operate on multiple processes, like "kill", will then apply over the list of tagged processes, instead of the currently highlighted one.

U Untag all processes (remove all tags added with the Space key).

F9, k "Kill" process: sends a signal which is selected in a menu, to one or a group of processes. If processes were tagged, sends the signal to all tagged processes. If none is tagged, sends to the currently selected process.

Not quite the answer you were looking for, but close. You can also eliminate process groups or children with kill, see:

http://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes

share|improve this answer
add comment

From the htop manpage:

F5, t Tree view: organize processes by parenthood, and layout the relations between them as a tree. Toggling the key will switch between tree and your previously selected sort view. Selecting a sort view will exit tree view.

So hit F5, and you may have to then hit 't'. That'll let you kill the entire tree.

share|improve this answer
    
This will only work if the "tree" is inevitably bound to its parent; note that htop shows thread groups as trees (in which case, the branches are not real processes). Otherwise, the orphan children will be re-parented by init, so not killed. –  TAFKA 'goldilocks' Feb 5 '13 at 20:36
    
I absolutely agree, though this depends strictly on the processes involved. Killing an ssh tree session will kill underlying children procs, for example; the OP wasn't particularly specific about why he wanted to kill a whole tree. –  Stephan Feb 5 '13 at 20:59
add comment

You could hijack htop's s command. In htop, pressing s invokes strace -p <the-pid-of-the-selected-process>.

Then what you could do is create a strace command that contains something like:

#! /bin/sh -
pids=$(ps -eo pid= -o ppid= |
  awk -v exclude="$PPID" -v pid="$2" '
    function descends(p) {
      if (p == exclude) return 0
      if (p == pid) return 1
      if (p <= 1) return 0
      return descends(parent[p])
    }
    { parent[$1]=$2 }
    END {
      for (p in parent)
        if (descends(p))
         print p
    }'
)
if [ -z "$pids" ]; then
  echo >&2 "No process found to kill"
else
  echo >&2 Killing $pids
  kill $pids
fi

And place it in some directory like ~/.htop-hijack, and call htop as:

PATH=~/.htop-hijack:$PATH htop

Then, pressing s would invoke that command instead of strace (you need to press Esc to return to the main screen afterwards).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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