top -c command in linux to filter processes listed based on processname

Go To StackoverFlow.com

113

top -c

Top lists all the processes, there are good options to filter the processes by username by using the option -u but I am wondering if there is any easy way to filter the processes based on processname listed under COMMAND column of the top output.

For Example I would want like top -some option -substring of processname and top displays pids only having this substring in its command name

2012-08-22 14:39
by NoName


173

Using pgrep to get pid's of matching command lines:

top -c -p $(pgrep -d',' -f string_to_match_in_cmd_line)

top -p expects a comma separated list of pids so we use -d',' in pgrep. The -f flag in pgrep makes it match the command line instead of program name.

2012-08-22 14:49
by perreal
Awesome. worked like a charm. Thanks a lo - NoName 2012-08-22 15:12
just remember that if new processes are spawned for 'stringtomatchincmd_line' they will not show up in top; you'd have to quit top and re-run this comman - eugenevd 2013-04-19 15:53
As eugenevd said, this will not work for new processes started after the top is called. Is there any way to do so? I need to run several programs with the same prefix on its name, and I would love to see how many of them are running in a live way, not needing to execute a new top (or just a ps) every time - Jester 2014-06-18 15:25
@Jester, eugenevd, I had the same problem as you did. I made a new version at http://stackoverflow.com/a/27653841/681830, hope that helps - Val 2014-12-26 06:58
I encountered top: pid limit (20) exceeded so I used this: top -c -p $(pgrep -f string_to_match_in_cmd_line | head -20 | tr "\\n" "," | sed 's/,$//') - f01 2016-11-29 14:27


86

It can be done interactively

After running top -c , hit o and write a filter on a column, e.g. to show rows where COMMAND column contains the string foo, write COMMAND=foo

If you just want some basic output this might be enough:

top -bc |grep name_of_process
2014-06-17 21:25
by nos
This gives me 'invalid option or syntax: -bc - shmim 2015-05-16 16:14
top -bc -n 1 is more convenient, as it limits the number of iterations to 1 - galath 2017-07-26 13:10
This should be the accepted answer. It's more acurate and works just with the options provided by 'top' command. And it does show new processes - Abraham TS 2018-01-19 14:40
The processes keep on disappearing and reappearing with top -c , hit o and write a filter on a column - MrR 2018-11-07 14:30


60

You can add filters to top while it is running, just press the o key and then type in a filter expression. For example, to monitor all java processes use the filter expression COMMAND=java. You can add multiple filters by pressing the key again, you can filter by user with the u key, and you can clear all filters with the = key.

2016-01-09 01:58
by Don Kirkby
This is a better solution than the accepted answer (this solution posed 3 years later than the original). Unlike the original, this solution actually works on newly spawned processes as well. Very important if you're monitoring Apache processes that are created on-demand - Nate Lampton 2016-03-16 21:00
Indeed a better solution because it filters live. Accepted solution resolves process ids before running top. Resulting process is like top -c -p 920,1345,1346 wich is fixed - Jérôme Gillard 2016-05-23 10:34
Using o as a filter does not exist in some (older) versions of top: top: procps version 3.2.8 User filter exists, so that does work - Manwe 2017-11-21 08:24


11

@perreal's command works great! If you forget, try in two steps...

example: filter top to display only application called yakuake:

$ pgrep yakuake
1755

$ top -p 1755

useful top interactive commands 'c' : toggle full path vs. command name 'k' : kill by PID 'F' : filter by... select with arrows... then press 's' to set the sort

the answer below is good too... I was looking for that today but couldn't find it. Thanks

2014-06-17 20:50
by BBW Before Windows


7

After looking for so many answers on StackOverflow, I haven't seen an answer to fit my needs.

That is, to make top command to keep refreshing with given keyword, and we don't have to CTRL+C / top again and again when new processes spawn.

Thus I make a new one...

Here goes the no-restart-needed version.

__keyword=name_of_process; (while :; do __arg=$(pgrep -d',' -f $__keyword); if [ -z "$__arg" ]; then top -u 65536 -n 1; else top -c -n 1 -p $__arg; fi; sleep 1; done;)

Modify the __keyword and it should works. (Ubuntu 2.6.38 tested)

2.14.2015 added: The system workload part is missing with the code above. For people who cares about the "load average" part:

__keyword=name_of_process; (while :; do __arg=$(pgrep -d',' -f $__keyword); if [ -z "$__arg" ]; then top -u 65536 -n 1; else top -c -n 1 -p $__arg; fi; uptime; sleep 1; done;)
2014-12-26 06:54
by Val
this is missing an overview about the system's workload. add "uptime" before sleep, for short process lists, this is sufficient IMHO - Sevyls 2015-02-16 17:02
@Sevyls Thanks for pointing out! After other few attempts (that not work as good as) your version might be one of the best one. Answer modified - Val 2015-02-24 05:37
Works great if I have one or more processes running that match but I get a "top: Invalid user" when all stop. It does recover when another process starts. I'm running on Raspbian FYI. Thanks - Mike 2015-10-01 13:57


1

what about this?

top -c -p <PID>
2012-08-22 16:32
by Satish
we are trying to filter by processname(or its substring) and when you restart your process you will get a new pid - Srujan Kumar Gulla 2012-08-22 18:05


1

Most of the answers fail here, when process list exceeds 20 processes. That is top -p option limit. For those with older top that does not support filtering with o options, here is a scriptable example to get full screen/console outuput (summary information is missing from this output).

__keyword="YOUR_FILTER" ; ( FILL=""; for i in  $( seq 1 $(stty size|cut -f1 -d" ")); do FILL=$'\n'$FILL; done ;  while :; do HSIZE=$(( $(stty size|cut -f1 -d" ")  - 1 ));  (top -bcn1 | grep "$__keyword"; echo "$FILL" )|head -n$HSIZE; sleep 1;done )

Some explanations

__keyword = your grep filter keyword
HSIZE=console height
FILL=new lines to fill the screen if list is shorter than console height
top -bcn1 = batch, full commandline, repeat once
2017-11-21 08:45
by Manwe


0

I ended up using a shell script with the following code:

#!/bin/bash

while [ 1 == 1 ]
do
    clear
    ps auxf |grep -ve "grep" |grep -E "MSG[^\ ]*" --color=auto
    sleep 5
done
2015-11-10 14:21
by Jester
Ads