Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to show top five CPU consuming processes with ps?

share|improve this question
up vote 37 down vote accepted

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"
share|improve this answer
ps aux | sort -k 3,3 | tail -n 5 ;-) – PlanasB May 28 '11 at 19:01
it will be good to include the numeric sort, to skip the header of ps aux. ps aux | sort -nrk 3,3 | head -n 5 – syss May 12 at 10:42

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)

share|improve this answer
This doesn't sort xx.x and x.x digits correctly. I get a set of processes in this cpu usage order: %CPU PID USER COMMAND 6.9 3295 gearond /usr/lib/firefox/plugin-container /usr/lib/adobe-flashplugin/libflashplayer.so -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 3216 true plugin 3.7 13665 gearond gnome-terminal 2.8 13672 gearond bash 2.6 1444 root /usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch -background none 12.5 3216 gearond /usr/lib/firefox/firefox – Dennis Mar 15 '15 at 16:02
right you are @Dennis. Good catch. I've updated my answer to correctly handle this. – Ryan Horrisberger Mar 16 '15 at 20:28
"more readable" result, but very ugly command.. – Hernán Eche Jul 22 '15 at 19:26

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
share|improve this answer
This IS the correct answer. Thank you Facundo. I was trying to explain to others that you can use sort and pipe it, but ps also comes built in with an option for sorting and now I see you also use it which is great. – Luis Alvarado Apr 7 at 16:01

I don't think ps is what you are looking for. Have you looked at the output from top?

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.

share|improve this answer
In OS X, is top -o cpu -n 5 a way of achieving the same thing. Does anyone know? My top is different to your top. – boehj May 29 '11 at 4:00
You're the one with the different top so you would be in a position to say. My top doesn't have -o and -n sets the number of times it refreshes the display before quitting. – Caleb May 29 '11 at 5:28
Fair enough. I'll get on a Linux box this afternoon and take a look. My top doesn't seem to have a batch mode, which is quite limiting. There must be some way of piping top into other commands. I'll do some research. – boehj May 29 '11 at 5:45
I mentioned the batch mode switch in my answer but it's actually unnecessary for my top because it auto-detects being part of a pipe instead of an interactive session. Did you try just piping it without that? – Caleb May 29 '11 at 5:56
Will do. GNU top worked as described this afternoon. Cheers. – boehj May 29 '11 at 12:05

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

share|improve this answer

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
share|improve this answer

The command line tool ps has its own sort option, so I prefer:

$ ps -eo pcpu,args --sort=-%cpu | head

You can add the columns you want. See what other options are available via the ps man page.

$ man ps
share|improve this answer

Quickest one liner I have found for this (note 6 because the header adds up):

ps aux k-pcpu | head -6
share|improve this answer

top -bn1 |sed -n '7,12'p works as a nice little one liner too.
Though I do prefer to use ps with the --sort=X -o X,Y,Z

using ps you can pull different stats grep for a process OR user and then total OR avg them with a pipe to awk.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.