Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

top -c command in linux to filter processes listed based on processname

People also ask

How do you filter a top by process name?

To filter the top output to a specific process, press the O key and enter the entry as COMMAND=name, where the name refers to the process name. Press ENTER, and the top utility will filter the processes to systemd only. You can also highlight the specific process while keeping other processes in view.

How do I find the top 5 processes in Linux?

Like top command htop utility within linux which will help you to find the top cpu consuming processes in Linux. To find it out use “htop” command.

How can you get the list of processes running on the server in Linux?

To list currently running processes, use the ps , top , htop , and atop Linux commands. You can also combine the ps command with the pgrep command to identify individual processes.


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.


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