Limit the output of the TOP command to a specific process name
Asked Answered
S

18

84

If you call the top command, you get all the running processes. But how can I limit the output only to a certain process name like "java"?

I've tried this top -l 2 | grep java but in this way you get only snapshots and not a continuously updated list. And top -l 0 | grep java is not really clear.

Snead answered 16/9, 2010 at 14:42 Comment(2)
Unfortunately so far there is still no valid answer as -p & -b are not supported by OS X' top command.Snead
I've posted what's maybe the first OSX solution below - and just in the nick of time ;)Conflagrant
P
132

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
Piassava answered 16/12, 2011 at 15:8 Comment(12)
Best answer, improves on dogbane's so you can actual type the name rather than pasting lots of PIDs. (And this is SO)Busty
I'd vote you up multiple times if I could, this should really be the "accepted" answer, I think.Ritch
I'm getting top: pid limit (20) exceeded. Is there anyway around that limit?Donough
@celwell, please try top -p $(pgrep process-name | head -20 | tr "\\n" "," | sed 's/,$//'). This will show data for up to the first 20 PIDs returned by pgrep.Merthiolate
See #25066598 for a breakdown of the full syntax of this command.Humphreys
As of Yosemite (or earlier?) it’s -pid instead of -p.Bombsight
The "pid limit (20) exceeded" message is from the constant, MONPIDMAX, which is most likely a throwback to itsy-bitsy terminals, and should probably be updated. According to this question, this constant can be raised to sensible values without apparent issue, assuming you can compile your own version of top.Hydrotaxis
Great answer, thanks! Is there a way to modify it, in order to make possible displaying few process names?Suspiration
Try out htop instead of top. I think it's much easier to use and shows the full process name by default.Inaccurate
How about this if you want to use full name of the process: top -p `ps aux | grep <myfilename.py> | awk -F ' ' '{print $2}' | tr "\\n" "," | sed 's/,$//g'`Seif
instead of tr and sed, you can use paste -s -d, -Dionysus
This command can be greatly simplfied by using pgrep's --delimiter option: top -p $(pgrep -d , process-name)Fireguard
B
56

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 COMMAND
18884 user  25   0  672M  95M  9476 S     0.0  1.1   0:02   1 java
18892 user  25   0 2280M 123M 12252 S     0.0  1.5   0:05   1 java
18919 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.)

Browband answered 16/9, 2010 at 15:25 Comment(1)
On Mac it should be "top -pid ID" but the Java process may have several distinct IDs as there may be several java processes.Snead
T
32

how about top -b | grep java

Transilluminate answered 16/9, 2010 at 14:48 Comment(1)
Doesn't work on Mac because Mac uses BSD tools and not GPL tools. And there are some differences. I suppose the "-b" version is similar to "-l 0" but also the header of the table is printed?Snead
R
30

Expanding on @dogbane's answer, you can get all the PIDs for a named process with pgrep to do the following:

top -p "$(pgrep -d ',' java)"
Renner answered 15/5, 2013 at 12:48 Comment(3)
This has a max limit of 20 pidsZoba
top -p "$(pgrep -d ',' php)" top: pid limit (20) exceededZoba
You're right! man top says "This option can be given up to 20 times, or you can provide a comma delimited list with up to 20 pids." Well dang.Renner
F
7

Using the answer from here I was able to create a one liner

top -pid $(pgrep process_name | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ -pid /g')

This works for me on MacOS 10.12 (Sierra)

Function answered 9/1, 2017 at 18:14 Comment(0)
A
6

Use the watch command

watch -d 'top -n1 | grep mysql'
Assuan answered 16/9, 2010 at 14:58 Comment(7)
Should be "watch --d". However I get no output executing this command.Snead
-1: man watch: "watch runs command repeatedly, displaying its output and errors"; it does not show currently running processes like top doesHide
@AlexanderOrlov, there is no special magic here, just use top -n1 to print and exit, and use watch to monitor it continuously.Assuan
@sdaau, your comment is irrelevant to my answer, please reply more carefully next time.Assuan
I think we should add -b options. watch -d 'top -b -n1 | grep mysql'Larceny
I think we should add -b options. watch -d 'top -b -n1 | grep mysql'Larceny
@Hide top -n1 runs just once, watch makes sure it runs repeatedly. This is the only way to also watch processes with the name mysql that are started after you run this program.Inglebert
S
2

I solved my problem using:

top -n1 -b | grep "proccess name"

in this case: -n is used to set how many times top will what proccess
and -b is used to show all pids

it's prevents errors like : top: pid limit (20) exceeded

Simmonds answered 25/2, 2014 at 19:6 Comment(0)
B
2

The following code updates a list of processes every 5 seconds via the watch command:

watch -n 5 -t top -b -n 1 -p$(pgrep java | head -20 | tr "\\n" "," | sed 's/,$//')

Balas answered 18/6, 2014 at 14:8 Comment(0)
C
2

I run it (eg.): top -b | egrep -w 'java|mysqld'

Cotta answered 1/9, 2015 at 13:0 Comment(0)
P
1

Suppose .. if we have more than 20 process running on the server with the same name ... this will not help

top -p pgrep oracle | head -n 20 | tr "\\n" "," | sed 's/,$//'

It will try to list and provide real time output of 20 process where we have good chance of missing other prcesses which consumes more resource ....

I am still looking for better option on this

Pileup answered 31/3, 2014 at 17:9 Comment(0)
L
1

You need to feed the output of pgrep {proc_name} to top -pid {pid_No}. The problem with top -pid is that it expects -pid before each pid you want to monitor.

On Mac in zsh I can handle this problem e.g. like that:

top `pgrep proc_name | awk '{printf " -pid %d",$1}'`

It's not ideal too, because pgrep looks for substrings in process names. E.g. pgrep dd can return results for icdd, hidd, cloudd etc. The option pgrep -x should return the exact matches only (like grep -w). But it doesn't work for me in Mac Terminal, although it does in Ubuntu virtual machine.

Libove answered 24/10, 2021 at 22:24 Comment(1)
WOW This is AWESOME. To top multiple processes of the same name on MACOS!Plowman
S
0

A more specific case, like I actually was looking for:

For Java processes you can also use jps -q whereby jps is a tool from $JAVA_HOME/bin and hence should be in your $PATH.

Snead answered 20/5, 2014 at 7:10 Comment(0)
G
0

Running the below will give continuous update in console:

bcsmc2rtese001 [~]$ echo $SHELL  
/bin/bash  
bcsmc2rtese001 [~]$ top | grep efare  or watch -d 'top | grep efare' or top -p pid
27728 efare     15   0 75184 3180 1124 S  0.3  0.0 728:28.93 tclsh  
27728 efare     15   0 75184 3180 1124 S  0.7  0.0 728:28.95 tclsh
Gottuard answered 20/5, 2014 at 7:54 Comment(0)
C
0

I came here looking for the answer to this on OSX. I ended up getting what I wanted with bash and awk:

topfiltered() {
  [[ -z "$1" ]] && return
  dump="/tmp/top_dump"
  rm -f "$dump"
  while :; do
    clear
    [[ -s "$dump" ]] && head -n $(( $LINES - 1 )) "$dump"
    top -l 1 -o cpu -ncols $(( $COLUMNS / 8 )) | awk -v p="$(pgrep -d ' ' $@)" '
        BEGIN { split(p, arr); for (k in arr) pids[arr[k]]=1 }
        NR<=12 || ($1 in pids)
    ' >"$dump"
  done
}

I loop top in logging mode and filter it with awk, building an associative array from the output of pgrep. Awk prints the first 12 lines, where line 12 is the column headers, and then every line which has a pid that's a key in the array. The dump file is used for a more watchable loop.

Carpetbagger answered 19/9, 2014 at 18:12 Comment(1)
On OSX you could use something like pgrep <process_name> ; top -pid <pid_specified_by_pgrep>Dodgson
K
0

just top -bn 1 | grep java will do the trick for you

Kailakaile answered 18/11, 2014 at 12:5 Comment(0)
C
0

Here's the only solution so far for MacOS:

top -pid `pgrep java | awk 'ORS=" -pid "' | sed 's/.\{6\}$//'`

though this will undesirably report invalid option or syntax: -pid if there are no java processes alive.

EXPLANATION

The other solutions posted here use the format top -p id1,id2,id3, but MacOS' top only supports the unwieldy format top -pid id1 -pid id2 -pid id3.

So firstly, we obtain the list of process ids which have process name "java":

pgrep java

and we pipe this to awk which joins the results with delimitor " -pid "

| awk 'ORS=" -pid "'

Alas, this leaves a trailing delimitor! For example, we may so far have obtained the string "123 -pid 456 -pid 789 -pid ".

We then just use sed to shave off the final 6 characters of the delimitor.

| sed 's/.\{6\}$//'`

We're ready to pass the results to top:

top -pid `...`
Conflagrant answered 10/3, 2019 at 2:54 Comment(0)
P
0

get pid of process:

# pidof <process>

tell top what process pid(s) to display

# top -p <pid1>,<pid2>, etc

example:

landis@linux-e6510:~>pidof konsole
1841 1709
landis@linux-e6510:~>top -p 1841,1709

Top will only display the 2 'konsole' processes. This is useful on a busy server via ssh, not having to pipe thru grep.

Propensity answered 28/12, 2019 at 4:35 Comment(1)
sorry, didn't see the macos tag under question. mine works on all my linux boxes.Propensity
C
-1

Using the approach mentioned in the answer by Rick Byers:

top -p `pgrep java | paste -sd "," -`

but I had more than 20 processes running so following command can be helpful for someone who encounter a similar situation.

top -p `pgrep java | head -n 20 | paste -sd "," -`

pgrep gets the list of processes with given name - java in this case. head is used to get first 20 pids because top cannot handle more than 20 pids when using -p argument. Finally paste joins the list of pids with ','.

You can control the process name you are looking for in the above command and the number of processes with that name you are interested to watch. You can ignore the head -n 20 part if the number of your processes with the given name is less than 20.

Cynicism answered 30/1, 2018 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.