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

  • 1
    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.
    – bwDraco
    Feb 9 '15 at 15:39
  • 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...) Feb 9 '15 at 15:44

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
  • 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? Feb 24 '16 at 12:44

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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