Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top command to show the %cpu in descending order

I would like to have the cpu percentage in decending order. When I give the command :

top -bn 1 | grep "^ " | awk '{ printf("%-8s  %-8s \n", $2, $9); }' | head -8

It shows processes which are not the top most using CPU.

like image 501
Azaksai Avatar asked May 30 '14 14:05

Azaksai


People also ask

How do I sort my CPU at the top?

Sorting the top output By default, top sorts its entries by the CPU usage. You can change that by pressing M to sort by memory usage. To reverse sort your output, press R. To return to sorting by CPU, press P.

What is %CPU in top command?

The top command calculates the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. For example, suppose we set two seconds as the refresh interval, and the CPU usage reports 50% after a refresh.

How do I find the top 10 processes in Linux?

The ps command command displays every process ( -e ) with a user-defined format ( -o pcpu ). First field is pcpu (cpu utilization). It is sorted in reverse order to display top 10 CPU eating process.


3 Answers

Run top as a process (I'm using Ubuntu 14.04)

top

Once in top...

P <- Sort by CPU usage

M <- Sort by MEM usage

z <- Add cool visual colors

x <- Highlight column you are currently sorting by

like image 178
Andrew Cassidy Avatar answered Oct 26 '22 08:10

Andrew Cassidy


In your command, you have

grep "^ "

which filters out lines that do not start with a space.

With this, you're filtering out processes that have PIDs longer than 4 characters, since the top command left pads the PIDs to 5 characters.

Use grep "^[0-9 ]" instead.

like image 31
Othi Avatar answered Oct 26 '22 09:10

Othi


Try top with the -u flag: top -u

like image 24
S. Friese Avatar answered Oct 26 '22 10:10

S. Friese