top -c command in linux to filter processes listed based on processname
LinuxUnixProcessTop CommandLinux Problem Overview
top -c
Top lists all the processes, there are good options to filter the processes by username by using the option -u
For Example I would want like top -some option -substring of processname and top displays pids only having this substring in its command name
Linux Solutions
Solution 1 - Linux
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.
Solution 2 - Linux
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
Solution 3 - Linux
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 =.
Solution 4 - Linux
@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
Solution 5 - Linux
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;)
Solution 6 - Linux
In htop
, you can simply search with
/process-name
Solution 7 - Linux
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
Solution 8 - Linux
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
Solution 9 - Linux
what about this?
top -c -p <PID>
Solution 10 - Linux
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
Solution 11 - Linux
For anyone on a Mac, where top doesn't support the kind of filtering shown in other answers (and the pgrep args are slightly different)... This function will launch top for the processes matching the pattern in the first arg (according to pgrep), and with any other args passed to top.
function topnamed() {
name=$1
shift
top -pid $(pgrep -d ' -pid ' -fi "$name") 99999999 $@
}
(The "i" in "-fi" makes it case-insensitive.)
Basic example showing any "python" processes:
topnamed python
Example with additional args for top:
topnamed python -o mem
Unless I'm missing something, pgrep (at least in the current version of MacOS) adds a trailing delimiter, even though the man page says it's "to be printed between each". So the 99999999 at the end is a dummy value to keep it from blowing up. (Maybe there's better workaround.)
It has the downside (mentioned in other answers) of only including the processes at launch time.