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.
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.
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" :-)
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"'
You could always do "top | grep python" but I'm assuming you want something more dynamic
top -d[count]
(at least on FreeBSD) to get [count] displays.
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.