top -c command in linux to filter processes listed based on processname
-
27-06-2021 - |
Question
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
Solution
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.
OTHER TIPS
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
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 processes containing the string "java", use the filter expression COMMAND=java
.
You can add multiple filters by pressing o again.
You can filter by user with u. Clear all filters with =.
@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;)
In htop
, you can simply search with
/process-name
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
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
what about this?
top -c -p <PID>
This expect script will filter processes by name and show newly created ones. It is basically automate the user interaction with top by sending 'o' and 'COMMMAND=my_program' for you. similar to @nos Answer.
file: topname.exp
#!/usr/bin/expect --
if {[llength $argv] < 1 } {
send_user "Usage: topname process_name top_cmd_args \n"
exit 1
}
set keyword [lindex $argv 0]
spawn top {*}[lrange $argv 1 end]
expect {
-re .
{
send "o\r"
expect "*add filter*"
send "COMMAND=${keyword}\r"
interact
}
}
So you would use it like:
./topname.exp my_program
./topname.exp java # this filters java processes
Also it passed other flags that top accepts like -u e.g.
./topname.exp java -u root # this filters java processes by root user
./topname.exp java -u root -d 1 # this filters java processes by root user and delay top update by 1 second