Top command to sort by cpu/memory

top is command in to view processin running in a Linux machine.

Its will get dynamic real-time view of all process running in a Linux machine.

Run top command in your Linux machine and output should look like below:

$ top


Tasks: 233 total, 3 running, 230 sleeping, 0 stopped, 0 zombie
Cpu(s): 55.5%us, 3.9%sy, 0.0%ni, 40.5%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 5880784k total, 5432556k used, 448228k free, 169636k buffers
Swap: 9764860k total, 0k used, 9764860k free, 1315972k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2368 guest 20 0 996m 216m 47m R 96 3.8 420:25.79 thunderbird
2431 guest 20 0 1702m 717m 54m S 11 12.5 38:10.37 firefox
1106 root 20 0 186m 25m 9.8m S 7 0.4 22:45.97 Xorg
6243 guest 20 0 731m 36m 25m R 2 0.6 0:04.93 yakuake
3422 guest 20 0 1029m 191m 23m S 1 3.3 2:43.51 chrome
1692 guest 20 0 752m 21m 14m S 0 0.4 0:07.87 gnome-settings-
2502 guest 20 0 748m 143m 45m S 0 2.5 2:56.74 chrome
2640 guest 20 0 928m 84m 21m S 0 1.5 0:08.87 chrome
2781 guest 20 0 972m 124m 21m S 0 2.2 1:38.58 chrome
2907 guest 20 0 999m 60m 12m S 0 1.1 1:00.70 chrome
3352 guest 20 0 928m 72m 21m S 0 1.3 0:06.46 chrome
6286 guest 20 0 1290m 41m 26m S 0 0.7 0:00.45 knotify4
1 root 20 0 24456 2432 1356 S 0 0.0 0:00.68 init
2 root 20 0 0 0 0 S 0 0.0 0:00.18 kthreadd

By default top output is sorted with CPU usage.

There are lot of options available with which we can sort top output based on our requirement.

To view all available options run top command and then press shift + o OR O,  it will display all possible fields which can used to sort.

If you want to sort all processes with memory usage run top command then press “O” and then press “n” and enter. Now the output is sorted and displayed based on memory consumption of a process.

To quit from from top’s real time view just press “q”.

Ubuntu 14.04 tip to sort by cpu/memory:

Ubuntu 14.04 comes with top 3.3.9 version where shift + o OR O won’t work.

Here you need to use F to sort with cpu/memory. First run top command, press F then select cpu/memory by using up/down arrows and press to set sort, use Esc/q to end.

This will sort top output according to your selection.

-Sany

Advertisements

4 comments on “Top command to sort by cpu/memory

  1. Hi Sany i need to get email alert if %CPU is above 95% for any current running services can help me for this script…

    • Hi MutuKumar,

      following command will works for you

      ps aux | awk ‘{print $3″\t”$11}’ | grep -v “%CPU” | sort -nr | awk ‘{if($1 > 95) {print “( “$1″\t”$2″)”}}’

      I tested it Ubuntu 12.04

      • hi Sandeep,

        Thank you so much, by using your guidance i done this….

        top -b -n1 | awk ‘{print $9″\t”$10″\t”$12}’ | grep -v “%CPU” | sort -nr | awk ‘{if($1 >

        95) {print $1″\t”$2″\t”$3}}’ > file1

        cat file1 | mail -s “%CPU >= 0” kumarmuthu989@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s