I'm trying to get an accurate read of my used CPU (in percent) from top. This is the command I'm running for testing:

top -n1 | awk '/Cpu\(s\):/ {print $2}'

This returns:


Which is the proper piece of data I want. However, every time I run the command I get the same output, even though I am applying different loads on my system (and not to mention htop tells me my usage is different). It seems that whenever I start top, my CPU usage is the same. Only after a couple of frames does it give me proper values.

It doesn't seem like I can parse top's output this way, so I'm looking for other reliable applications which will give me an accurate reading from the shell. I really like how htop can give me a per-core reading.

I've tried iostat and mpstat but they seem to give inaccurate and "slow to change" values.

  • 2
    Don't. The proper question to ask is: How do I obtain top-like CPU usage statistics from the shell? Commented Dec 17, 2011 at 20:33
  • @StéphaneGimenez thanks I modified my question
    – nopcorn
    Commented Dec 17, 2011 at 20:37

4 Answers 4


I use this script (from this thread on the Arch boards):

read cpu a b c previdle rest < /proc/stat
sleep 0.5
read cpu a b c idle rest < /proc/stat
CPU=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
  • Linux has some great info in /proc/ -- see linux.die.net/man/5/proc for all the goodies. Just beware that these are Linux only. There's really no cross-platform way to get that information unless you use a library like Sigar.
    – Pat Notz
    Commented Dec 19, 2011 at 17:16
  • The idea is good, but some details are incorrect. The idle value is the 5th column, and you should also add the iowait column (6th column) to get a reasonable percentage. To be fully correct, you'd have to add all the "rest" values as well, but they are often very small. Commented Feb 7, 2013 at 16:44
  • Idle value is the fifth column, if you are counting the "cpu" column. Otherwise, it is the fourth.
    – SunSparc
    Commented Dec 30, 2014 at 23:08

Check out sar, as well. Implementations can vary widely from nix to nix, but it should give you basic system stats, at given snapshots. I'm not sure how accurate the values are at the point at which the command is first initialized, but you might play around to see how it compares to top, iostat, etc.

The output is column-based, like top, so you should be able to pipe output to awk or cut to manipulate the results.

  • I've tried sar before with mixed results. I'm going to go with @jasonwryan's answer here because I can easily modify it to represent usage with both of my CPU cores.
    – nopcorn
    Commented Dec 17, 2011 at 21:11

Have you looked at collectl? It's handy because you can tailor the output to your needs. See: http://collectl.sourceforge.net/


What I found is similar to the person who asked the question above, at least on CentOS 6. If I run top in batch mode for just one iteration, it seems to collect the same figure, almost as if its tendency is to start with what it last remembers displaying. Top seems to need to accumulate at least a couple of figures in order to get deltas to give you correct percentages. Having tested over 10 iterations, I found that the second figure that popped up showed sufficient differentiation on each run. So, its easy enough to get this line via

top -b -n 2 | grep Cpu | tail -1

I like incorporating this with uptime for load and a grep filter of select processes from ps, such as postgres queries. A very simple monitor can be expressed with the alias:

alias postgresmon="watch -d 'uptime;top -b -n 2 | grep Cpu | tail -1;ps -ef | grep postgres\: | grep -v idle'"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .