How can I receive top-like CPU statistics from the shell?

11,299

Solution 1

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) ))

Solution 2

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.

Solution 3

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

Share:
11,299
n0pe
Author by

n0pe

Updated on September 18, 2022

Comments

  • n0pe
    n0pe over 1 year

    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.

    • Stéphane Gimenez
      Stéphane Gimenez over 12 years
      Don't. The proper question to ask is: How do I obtain top-like CPU usage statistics from the shell?
    • n0pe
      n0pe over 12 years
      @StéphaneGimenez thanks I modified my question
  • n0pe
    n0pe over 12 years
    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.
  • Pat Notz
    Pat Notz over 12 years
    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.
  • Peter Eisentraut
    Peter Eisentraut over 11 years
    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.
  • SunSparc
    SunSparc over 9 years
    Idle value is the fifth column, if you are counting the "cpu" column. Otherwise, it is the fourth.