Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

10.7%us,

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.

share|improve this question
2  
Don't. The proper question to ask is: How do I obtain top-like CPU usage statistics from the shell? – Stéphane Gimenez Dec 17 '11 at 20:33
    
@StéphaneGimenez thanks I modified my question – n0pe Dec 17 '11 at 20:37
up vote 12 down vote accepted

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

#!/bin/bash
read cpu a b c previdle rest < /proc/stat
prevtotal=$((a+b+c+previdle))
sleep 0.5
read cpu a b c idle rest < /proc/stat
total=$((a+b+c+idle))
CPU=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
share|improve this answer
    
Thanks, this is very accurate. – n0pe Dec 17 '11 at 21:08
    
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 Dec 19 '11 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. – Peter Eisentraut Feb 7 '13 at 16:44
    
Idle value is the fifth column, if you are counting the "cpu" column. Otherwise, it is the fourth. – SunSparc Dec 30 '14 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.

share|improve this answer
    
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. – n0pe Dec 17 '11 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/

share|improve this answer

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'"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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