9

I want to monitor only a process and its children processes on htop. Filtering on the name of the parent process lists only the parent process, not its children. How do I show the children processes too?

7

Under Linux, you can do:

htop -p `pstree -p $PID | perl -ne 'push @t, /\((\d+)\)/g; END { print join ",", @t }'`

where $PID is the root process. This works as follows:

  1. The list of the wanted processes are obtained with pstree, using the -p option to list them with their PID.
  2. The output is piped to a Perl script that retrieves the PID's, using a regular expression (here, \((\d+)\)), and outputs them separated with commas.
  3. This list is provided as an argument of htop -p.

For other OS like Mac OS, you may need to adapt the regular expression that retrieves the PIDs.

Note: It is unfortunately not possible to update the list with new children that are spawn later, because once htop has been executed, one cannot do anything else. This is a limitation of htop (current version: 2.0.2).

  • $PID can be the name of the process, or it has to be the id? – becko Feb 7 '15 at 18:23
  • @becko $PID has to be the pid, but you can get the id from the name of the process with pgrep. – vinc17 Feb 7 '15 at 18:25
  • 5
    this will not update when new children are spawned though... would love to use sth like it for monitoring only stuff in my tmux session – black_puppydog Sep 18 '15 at 10:06
  • @black_puppydog me too! – becko Apr 4 '16 at 20:33
  • On macOS with pstree from homebrew: htop -p `pstree -p $PID | perl -ne 'push @t, /--- (\d+) /g; END { print join ",", @t }'` – jpsim Jul 27 '17 at 19:12
0
htop -p $(ps -ef | awk -v proc=15305 '$3 == proc { cnt++;if (cnt == 1) { printf "%s",$2 } else { printf ",%s",$2 } }')

Use awk to create a comma separated list of process id's from the output of ps -ef passing the parent process id as proc and then passing this out to htop -p.

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.