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?
-
2There is an issue 76 in the new htop repo. Vote for it!Victor Sergienko– Victor Sergienko2021-03-26 23:08:48 +00:00Commented Mar 26, 2021 at 23:08
-
1Related: github.com/ClementTsang/bottom/issues/734Andrey Mishchenko– Andrey Mishchenko2022-05-19 15:45:48 +00:00Commented May 19, 2022 at 15:45
4 Answers
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:
- The list of the wanted processes are obtained with
pstree, using the-poption to list them with their PID. - 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. - 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).
-
$PIDcan be the name of the process, or it has to be the id?a06e– a06e2015-02-07 18:23:35 +00:00Commented Feb 7, 2015 at 18:23 -
@becko
$PIDhas to be the pid, but you can get the id from the name of the process withpgrep.vinc17– vinc172015-02-07 18:25:24 +00:00Commented Feb 7, 2015 at 18:25 -
8this will not update when new children are spawned though... would love to use sth like it for monitoring only stuff in my tmux sessionblack_puppydog– black_puppydog2015-09-18 10:06:22 +00:00Commented Sep 18, 2015 at 10:06
-
-
On macOS with
pstreefrom homebrew:htop -p `pstree -p $PID | perl -ne 'push @t, /--- (\d+) /g; END { print join ",", @t }'`jpsim– jpsim2017-07-27 19:12:44 +00:00Commented Jul 27, 2017 at 19:12
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.
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.
on macOS (prolly works on linux too), a potential workaround that works for me is to identify a search phrase for each process in the hierarchy and use htops FILTER pattern to search for each term separated by a |. this allows a live monitor that captures child processes too as long as part of your search pattern captures the child process.
so if you have the following process hierarchy you want to monitor:
top-process-A
\__ subprocess-B
\__ subsubprocess-C
\__ subsubsubprocess-D
use the FILTER pattern A|B|C|D.
If the patterns are less specific, this might capture some other processes you dont care about, but it will drastically reduce the number of visible processes in the current viewport. this might be untenable though the more processes you are trying to monitor, especially if they all have different names.
-
Welcome & thanks for contributing. I am anyhow afraid that this answer would produce suboptimal results compared with the 3 existing ones.MC68020– MC680202024-02-13 00:49:57 +00:00Commented Feb 13, 2024 at 0:49