Limit the output of the TOP command to a specific process name Limit the output of the TOP command to a specific process name unix unix

Limit the output of the TOP command to a specific process name


I prefer the following so I can still use top interactively without having to look up the pids each time I run it:

top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'`

Of course if the processes change you'll have to re-run the command.

Explanation:

  • pgrep process-name returns a list of process ids which are separated by newlines
  • tr "\\n" "," translates these newlines into commas, because top wants a comma-separated list of process ids
  • sed is a stream editor, and sed 's/,$//' is used here to remove the trailing comma


Find the pids of the processes you want to monitor and then use the -p option which allows you to provide a list of pids to the top command.

Example:

top -p 18884 -p 18892 -p 18919  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND18884 user  25   0  672M  95M  9476 S     0.0  1.1   0:02   1 java18892 user  25   0 2280M 123M 12252 S     0.0  1.5   0:05   1 java18919 user  22   0 1492M 198M 28708 S     0.0  2.4   0:07   1 java

(I believe you can also pass in a comma-separated list.)