TransWikia.com

How to display only a process and its descendant processes on htop?

Unix & Linux Asked on December 3, 2021

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?

3 Answers

htop -p $(ps -ef | awk -v proc=$PID 'BEGIN{pids[proc]=1;printf "%s",proc} {if(pids[$3]==1){printf ",%s",$2; pids[$2]=1}}')

Where $PID is the root process id.

Use awk to create a comma separated list of the specified process and its descendant processes and pass the output to htop -p.

Answered by hnakamur on December 3, 2021

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.

Answered by Raman Sailopal on December 3, 2021

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).

Answered by vinc17 on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP