3

Is there an option for linux top command where i can filter processes by name?

For example, I only want to monitor python processes (there are several of them), and I'd like to do something like top -option "python" or something like that.

mgs
  • 135
  • 1
  • 4

4 Answers4

6

When you want information on processes, the answer is always ps

It is simple, and yet it has a ridiculous number of options.

Try this one:

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

Should give you the top 10, by cpu usage.

Satanicpuppy
  • 5,917
  • 1
  • 16
  • 18
  • In the OP's case you want to grep for python before sorting – voretaq7 Feb 08 '10 at 23:03
  • wow, ps rocks! This command did it: `watch "ps -eo pcpu,pid,user,args | grep python"`. Funny thing is that `watch` ends up in the output as well, because its command contains "python" :-) – mgs Feb 08 '10 at 23:15
  • try: watch "ps -eo pcpu,pid,user,args | grep [p]ython" – monomyth Feb 08 '10 at 23:22
2

This approximates the output of top:

watch 'ps axo pid,user,pri,nice,vsz,rsz,size,s,pcpu,pmem,time,cmd|grep "[p]ython\|PID USER"'
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
0

You could always do "top | grep python" but I'm assuming you want something more dynamic

g8keepa82
  • 33
  • 1
  • 1
  • 3
0

My Perl skills are basics, but to get a real Top filtered by name, save this code to a file called topn.pl:

#!/usr/bin/perl

shift @ARGV;
$name = shift @ARGV;
@pids = `/bin/ps -eo pid,user,args | /bin/grep   $name   | /bin/grep -v grep |   /usr/bin/tr -s " "  `;

$arg = "";
foreach (@pids) {
        $_ =~ /^\s([0-9]+)\s/;
        $pid = $1;
        $arg .= " p $pid " if $pid != "";
}

exec("/usr/bin/top $arg @ARGV");

Usage: topn.pl -n FOO c 2 where FOO is the process name to be be grep. The rest of the args are passed to top.

Top accepts at max 20 PID as arguments.

qux
  • 361
  • 2
  • 7