Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 –  maxmackie Dec 17 '11 at 20:37

3 Answers 3

up vote 10 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. –  maxmackie 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

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. –  maxmackie 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

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.