4

I'm running this command

grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'

Yet it only outputs something like 0.99xxxx%

If I do an apt-get upgrade or any process, I would imagine it would go above 1%. Even running stress -c 1 doesn't make it change any.

Is there a way to log CPU usage accurately? Server has 1 vCPU.

Need to have this log every 5 seconds.

while sleep 5; do "code" >> logfile; done
user1052448
  • 423
  • 2
  • 6
  • 19

3 Answers3

5

Why does cpu load not change more than a few hundredths?

Because /proc/stat is returning aggregated CPU load statistics since the system last booted, not real-time ones. If you run your script just after a reboot, the reported load might significantly change as long as the CPU load itself changes. However, the longer the script runs the lesser load changes will impact the displayed value and after a while, the value will essentially stay constant.

If you want to compute the load from /proc/stat and not use the already available tools that do it, you need to compute the difference from two consecutive samples, eg :

while sleep 5; do grep -w cpu /proc/stat ; done | \
    awk '{
        print (o2+o4-$2-$4)*100/(o2+o4+o5-$2-$4-$5) "%"
        o2=$2;o4=$4;o5=$5}'

Otherwise, a simpler but less accurate way might be:

vmstat -n 5 | \
    awk '{used=$13+$14;total=used+$15
          if(total>0) print used*100/total "%"}'
jlliagre
  • 29,783
  • 6
  • 61
  • 72
2

If you want to log the highest cpu percentage (this is, the process with the highest cpu usage in the moment of querying), you can use:

ps -e -o %cpu | tail -n +2 | sort -r | head -n 1 > cpu-usage.log

Explanation of the command:

  • ps -e -o %cpu gives you process information of every process in the output format that consist only in the cpu usage percentage

  • tail -n +2 filters the previous output starting from the second line (thus ignoring the header printed by ps)

  • sort -r sort the values in reverse order (highest first)

  • head -n 1 filters the data returned by sort so you discard all but the first line

higuaro
  • 15,730
  • 4
  • 36
  • 43
  • This seems to give more accurate results, however, how is it that I have 112% CPU with a single core? – user1052448 Aug 15 '15 at 21:12
  • Here you can find a nice explanation for that: http://serverfault.com/questions/522922/cpu-more-than-100-in-ps-aux – higuaro Aug 15 '15 at 23:15
  • And here you can find more info http://superuser.com/questions/174660/why-is-the-cpu-usage-reported-by-top-in-linux-over-100 – higuaro Aug 15 '15 at 23:16
  • According to his/her code, the OP is looking for the whole CPU usage, not the usage of the (single) highest CPU consumer. – jlliagre Aug 15 '15 at 23:38
  • In that case the question should be flagged as duplicated and the answer can be found here: http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux – higuaro Aug 16 '15 at 00:14
  • 1
    @higuaro It might indeed. The OP is using exactly the same questionable code that was lately added to one of the most upvoted replies there though. If he picked the code from that reply, he should have read the last comment! – jlliagre Aug 16 '15 at 01:14
0

Slightly modified #3 for my own uses:

for x in $(seq 1 11);do sleep 5;grep -w cpu /proc/stat ; done | \
awk '{
    print (o2+o4-$2-$4)*100/(o2+o4+o5-$2-$4-$5) "%"
    o2=$2;o4=$4;o5=$5}'

Where seq is the number of samples taken, Sleep is still the length of time between samples

  • While this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Apr 11 '16 at 09:24