Show top five CPU consuming processes with `ps`
Why use ps
when you can do it easily with the top
command?
If you must use ps
, try this:
ps aux | sort -nrk 3,3 | head -n 5
If you want something that's truly 'top'esq with constant updates, use watch
watch "ps aux | sort -nrk 3,3 | head -n 5"
The correct answer is:
ps --sort=-pcpu | head -n 6
So you can specify columns without interfering with sorting.
Ex:
ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6
Note for MAC OS X: In Mac OS X, ps
doesn't recognize --sort
, but offers -r
to sort by current CPU usage. Thus, for Mac OS X you can use:
ps -Ao user,uid,comm,pid,pcpu,tty -r | head -n 6
Depending on your needs you may find this a little more readable:
ps -eo pcpu,pid,user,args --no-headers| sort -t. -nk1,2 -k4,4 -r |head -n 5
sample output:
1.3 4 root [ksoftirqd/0]
1.1 9 root [ksoftirqd/1]
1.0 17606 nobody /usr/sbin/gmetad
1.0 13 root [ksoftirqd/2]
0.3 17401 nobody /usr/sbin/gmond
(the fields are %CPU,PID,USER,COMMAND)