How to show top five CPU consuming processes with ps?
The correct answer is:
ps --sort=-pcpu | head -n 6
So you can specify columns without interfering with sorting.
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
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)
I don't think
ps is what you are looking for. Have you looked at the output from
If you have GNU-Top, try using it's batch mode to spit out a process list sorted by cpu usage and using head/tail to get the top 5 lines (the first 8 are headers):
top -b -n 1 | head -n 12 | tail -n 5
The BSD top seems to behave differently and doesn't have a non-interactive mode, so use one of the other
ps based solutions.
Note that current versions of ps have sorting ability within them, based on field codes (given in the ps man page). The field code for processor usage is "c". You can use
--sort c at the end of a ps command e.g.
ps aux --sort c
This will put the process using the most cpu at the bottom of the list. Reverse order of the list by adding a minus to the field code used to sort e.g.
ps aux --sort -c
In order to add a point to other valuable answers, I prefer:
ps auxk-c | head -6
It also prints the header, which is nice.
k is identical to
c specifies CPU usage (alias
%cpu) field for sort, while
- is for reverse sort.
You may add more specifiers separated by
,, other possible specifiers are :
uid ... which you can find full list in STANDARD FORMAT SPECIFIERS section of man page. For example:
ps auxk -gid,-%mem | head -11
would print 10 processes with highest group id, internally sorted by memory usage.
top on Mac OS X has a logging mode option in the form of
top -l numberOfSamples (which seems to be the equivalent to the batch mode of GNU
top). It is necessary, though, to have at least two samples because "the first sample displayed will have an invalid %CPU displayed for each process, as it is calculated using the delta between samples" (
man 1 top).
# examples top -o cpu -l 2 -n 5 | tail -n 6 top -o cpu -l 2 -n 5 -stats pid,command,cpu | tail -n 6 top -o cpu -l 2 -n 5 -stats pid,command,cpu -U $(logname) | tail -n 6
I believe the simplest way to see top 5 cpu consuming process is,
ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head -n 5
To see top 5 memory consuming process is,
ps -eo pid,comm,%cpu,%mem --sort=-%mem | head -n 5
-e : This flag is used to select all process
-o : This flag is used to format as user-defined.
pid : This argument used for showing pid
comm: This argument used for showing command name only. To get full command use
%cpu: This argument shows the percentage of cpu utilization of the process in "##.#" format. Here
pcpu can also be used.
%mem: This argument shows the ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. Here
pmem can also be used.
--sort: Specify sorting order. After
- sign is used to sort highest value at the top where the default option which is
+ is to list increasing numerical order [i.e 0 to n].