Show top five CPU consuming processes with `ps`

How to show top five CPU consuming processes with ps?

Asked By: PlanasB

||

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"
Answered By: Boban P.

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.

Answered By: Caleb

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)

Answered By: Ryan Horrisberger

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
Answered By: ulf

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

Answered By: Cian D

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
Answered By: Facundo Victor

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

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

ps aux k-pcpu | head -6
Answered By: Nestor Urquiza

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.

Answered By: jshedler

top will display what is using your CPU. If you have it installed, htop allows you more fine-grained control, including filtering by—in your case—CPU.

Answered By: PSN

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.

Here k is identical to --sort and 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 : %mem, args, bsdstart, pid, gid, 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.

Answered By: 2i3r

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

Where,

-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 args, cmd or command

%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 = the - 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].

Answered By: arif

Here is something I came up with as I found the original answer a bit too arcane.
so, for bare bones, type

ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6

Let’s understand what each does. ps ofcourse shows a snapshot of current processes. -e shows every process on the system -o is to define the format we want the result in, as you can see we have specified the format as pid,cmd,%cpu,%mem, next --sort ofcourse, sorts. One important point to note here is that by default it sorts ascending. Also --sort needs the parameter to sort by, which we provide by -%cpu notice the - this is so that it sorts descending and we get the highest CPU usage first. Then we pipe this into head -n 6 which gives us the this

    PID CMD                         %CPU %MEM
   5995 transmission-gtk             5.1  1.3
 402083 /usr/lib64/firefox/firefox   4.2  6.7
 978875 /usr/lib64/firefox/firefox   3.6  4.0
   2982 /usr/bin/gnome-shell         2.7  3.0
   2774 /usr/libexec/Xorg vt2 -disp  1.9  1.0

Now that we understand the basics we can show off a little. For example you can use watch to update the list every 2 seconds like this

watch "ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6"

Or, for something fancier, you can install a python package called tabulate, type this in your terminal pip install tabulate, now you can really show off, using some sed fu etc

 ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 5 | tabulate -1 -f github | cut -f 2- -d "|" | sed '2s/----/    /' 

which gives this beautiful output –

    |    PID | CMD                        |   %CPU |   %MEM |
    |--------|----------------------------|--------|--------|
    |   5995 | transmission-gtk           |    5.1 |    1.3 |
    | 978875 | /usr/lib64/firefox/firefox |    4.5 |    4.1 |
    | 402083 | /usr/lib64/firefox/firefox |    4.2 |    6.7 |
    |   2982 | /usr/bin/gnome-shell       |    2.7 |    3   |

for ease of use and avoid typing this command over and over you can alias them into your .bashrc like this

alias top5="ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6"

after doing source .bashrc you can just type top5.
Or, you can just use htop and sort by %CPU htop also allows you to kill processes and much more.

Answered By: kontrol-c
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.