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
This question is related to
linux
unix
process
top-command
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>
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;)
@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
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
In htop
, you can simply search with
/process-name
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.
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
Source: Stackoverflow.com