Top -c on linux to filter processes listed based on process name

top -c 

All processes are listed, there are good options for filtering processes by username using the -u option, but I'm wondering if there is an easy way to filter processes based on the process name specified in the COMMAND column of the top output.

As an example, I would like to see only this substring in the name of my command as the top -substring option for the process name and top screen

+73
linux unix process top-command
Aug 22 2018-12-14T00:
source share
8 answers

Using pgrep to get the pid of the corresponding 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 the program name.

+125
Aug 22 2018-12-12T00:
source share

This can be done interactively.

After running top -c click o and write a filter in the column, for example. to display rows where the COMMAND column contains the string foo, write COMMAND=foo

If you just need some basic output, this might be enough:

 top -bc |grep name_of_process 
+53
Jun 17 '14 at 21:25
source share

You can add filters to top while you work, just press the o key, and then enter the filter expression. For example, to monitor all java processes, the filter expression COMMAND=java . You can add multiple filters by pressing the key again, you can filter the user with the u key, and you can clear all the filters with the = key.

+33
Jan 09 '16 at 1:58
source share

The @perreal team works great! If you forget, try two steps ...

example: filter top to display only an application called yakuake:

 $ pgrep yakuake 1755 $ top -p 1755 

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

The answer below is also good ... I searched for it today, but could not find it. Thanks

+8
Jun 17 '14 at 20:50
source share

After searching so many answers on StackOverflow, I did not see an answer to fit my needs.

That is, so that the top command continues to be updated with the given keyword, and we do not need to create CTRL + C / top again and again when new processes appear.

So I'm doing a new one ...

Here is the version without restarting.

 __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;) 

Change __keyword and it should work. (Verified by Ubuntu 2.6.38)

2.14.2015 added: Invalid part of the system workload with the above code. For people who care about the "average load":

 __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;) 
+6
Dec 26 '14 at 6:54
source share

how about this?

 top -c -p <PID> 
+1
Aug 22 '12 at 16:32
source share

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 
0
Nov 10 '15 at 14:21
source share

Most answers do not work here when the list of processes exceeds 20 processes. This is a top -p limitation. For those who have a senior vertex that does not support filtering with o parameters, here is an example script for getting a full screen / console (no summary information is available at 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 explanation

 __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 
0
Nov 21 '17 at 8:45
source share



All Articles