When I execute following command to get cpu usage , I get nice + user cpu usage.

top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}' 



Here I am getting problem is that the output depends on top command thus it doesn't change instantly as top command. So I am not getting correct cpu instantly. It gives same output and not changing.

I want to get realtime cpuusage in output. Please help me to improve my command.

  • 1
    And the question is? – Dennis Kaarsemaker Mar 26 '13 at 9:29
  • Try running the command prepended with time. On my system it only takes 0.165s. – depquid Mar 26 '13 at 18:16
  • Please give me edited command. – KK Patel Mar 28 '13 at 6:39
  • Try: time (top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}' ) – depquid Mar 28 '13 at 20:59
  • no diffrence in output,after tested in different systems – KK Patel Mar 29 '13 at 4:43

You're right, top appears to give incorrect CPU usage on first iteration. You can work around this issue like this:

top -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4}'

This will of course take twice as much time, but it will work anyway.

If you still want it work faster, you can use -d option, e.g. for 1-second total period of measurement use half of it:

top -d 0.5 -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4}'
  • serverfault.com/a/436499/134517 also it will take much more than twice the time as it will take roughly one second instead of nearly none. – altendky Sep 30 '16 at 15:47
  • In addition to the comment by altendky, see also answer by terdon for explanation why the issue is there in the first place. – Ruslan Sep 30 '16 at 16:01

Technically the first top doesn't always output the same value; it just outputs the average CPU load since last boot. Because top uses the delta of /proc/stat stats to calculate the CPU load, the first value is calculated while comparing against zero, yielding incorrect (but WAD) result.

If you don't want to use top, you can directly parse /proc/stat:

cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5) "%"}'

It's also faster and more accurate this way.

You can test it with stress:

stress -c 1 -q &

And do

killall stress


  • Nice. And if your command is placed in a file called 'cpu', then watch can be run on it: watch -n1 "bash ./cpu" – Brent Faust May 21 '15 at 5:31
  • Your awk script to parse /proc/stat is pretty cool but it appear to have the same drawback as top's first iteration: It outputs the average CPU load since last boot, not the current one. – pieroxy Feb 28 '16 at 23:01

Just to expand on @Ruslan's answer, top splits CPU usage between user, system processes and nice processes, we want the sum of the three. So, we can run top in batch mode which allows us to parse its output. However, as explained here, the 1st iteration of top -b returns the percentages since boot, we therefore need at least two iterations (-n 2) to get the current percentage. To speed things up, you can set the delay between iterations to 0.01:

top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'

Maybe use this one based on ps output :

ps aux  | awk 'BEGIN { sum = 0 }  { sum += $3 }; END { print sum }

Use watch command or for loop in shell,

i.e watch -n1 "top -b -n1 | grep 'Cpu(s)' | awk '{print \$2 + \$4}'"

  • Same thing Happens with watch – KK Patel Mar 26 '13 at 9:49

Why do you care about the CPU usage? The load average is a much better indicator:

cat /proc/loadavg

If you really need the CPU usage, parsing /proc/stat is probably the way to go.

  • 1
    Load average is basically a time integral of CPU usage (divided by time). Why would you say an integral characteristic is much better indicator [of everything] than a differential one when they have completely different use cases? Also, /proc/stat doesn't give CPU usage as it is, one has to read it twice with some time between reads and do some calculations to get CPU usage. This is just what top does. – Ruslan Sep 17 '13 at 11:41
  • I can't math, so I have to admit I don't really understand the difference between integral and differential :/ "Load average is basically a time integral of CPU usage" - is it really? AFAIK a process that hangs in the D state causes a load increase, but no CPU usage. "Why would you say..." - OP doesn't say what he really wants to accomplish, so to be honest, I just assume he wants to measure his system load and thought that the CPU usage would be the best indicator for this, so I directed him to load average. – Martin von Wittich Sep 17 '13 at 14:13
  • From your link: "Systems calculate the load average as the exponentially damped/weighted moving average of the load number." and "the system load is a measure of the amount of computational work". D state isn't taking any computational work. What is taking is the processes which use CPU time. So average load is basically percentage of time CPU wasn't idle for some period of time (say, minutes to tens of minutes). And CPU usage is percentage of time CPU isn't idle at time scale of milliseconds - i.e. updated every time you read it and reflecting current state. – Ruslan Sep 17 '13 at 14:22

Using sar command

sar 1 10

That means: get 10 times result in every second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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