Linux: Get CPU usage over past n minutes

14,787

Solution 1

You can use sar for that. For example sar 300 will be printing out the average CPU usage for the 5 minute periods.

~]# sar 300
Linux xxxxx....

10:40:02 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
10:45:02 AM     all     69.57      0.00      4.69      0.04      0.00     25.70
10:50:02 AM     all     68.99      0.00      4.61      0.07      0.00     26.33
10:55:02 AM     all     68.98      0.00      4.88      0.05      0.00     26.09

Solution 2

You want to look at /proc/loadavg. The first three fields are the one-minute, five-minute, and fifteen-minute load averages. For a built-in utility, look at the uptime command.

Share:
14,787

Related videos on Youtube

Fire Lancer
Author by

Fire Lancer

Updated on September 18, 2022

Comments

  • Fire Lancer
    Fire Lancer almost 2 years

    I want a script to tell me the average CPU usage over the past 5 minutes (and repeat every 5 minutes, likely as a cron job).

    /proc/stat just has counters since system start, and I want to avoid developing a full program to parse and sample that. top seems to just report usage over the moment (second?) before it displays/writes the table.

    sysstat/sar looks promising, but looks like I'll always loose the midnight value looking at what sar -u gives me...

    • bwDraco
      bwDraco over 9 years
      Unfortunately, we are not a code-writing service. Instead of simply asking for code to perform a particular task, please show us what you've tried so far (including any code you currently have) and where you're stuck so that we can help you with your specific problem. Questions that only ask for code are too broad and are likely to be put on hold or closed.
    • Fire Lancer
      Fire Lancer over 9 years
      the things I mentioned, /proc/stat, top, sar. I am not asking for the code (like I said, I don't really want custom/new code for this task), I am looking for an existing Linux utility / command line to get CPU usage info over a time period that works accross day boundaries etc (potentially along the lines of sar -u, but that misses the midnight value it seems...)
  • Fire Lancer
    Fire Lancer over 8 years
    does that work at just after 00:00:00 ? in my testing it never seemed to, because it would not combine the previous days data file fully? maybe some extra steps / tools / config?