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
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.
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
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
top -bc -n 1
is more convenient, as it limits the number of iterations to 1 - galath 2017-07-26 13:10
top -c , hit o and write a filter on a column
- MrR 2018-11-07 14:30
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.
top -c -p 920,1345,1346
wich is fixed - Jérôme Gillard 2016-05-23 10:34
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
@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
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;)
what about this?
top -c -p <PID>
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
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