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.
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.
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.
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.
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
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.
Try top with the -u flag: top -u
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With