Command to get CPU server load in % percentage using bash and /proc/stat on Linux

Wednesday, 11th March 2015

Reading Time: 2minutes

Command-to-get-CPU-server-load-in-percentage-using-bash-shell-script-and-linux-proc-stat

Getting load avarage is easy with uptime command, however since nowadays Linux servers are running on multiple CPU machines and Dual cores, returned load avarage shows only information concerning a single processor. Of course seeing overall CPU server load is possible with TOP / TLoad command  / HTOP and a bunch of other monitoring commands, but how you can get a CPU percentage server load using just  /proc/stat and bash scripting? Here is hwo:
 

:;sleep=1;CPU=(`cat /proc/stat | head -n 1`);PREV_TOTAL=0;for VALUE in "${CPU[@]}”; do let “PREV_TOTAL=$PREV_TOTAL+$VALUE”;done;PREV_IDLE=${CPU[4]};sleep $sleep; CPU=(`cat /proc/stat | head -n 1`);unset CPU[0];IDLE=${CPU[4]};TOTAL=0; for VALUE in “${CPU[@]}"; do let "TOTAL=$TOTAL+$VALUE"; done;echo $(echo "scale=2; ((($sleep*1000)*(($TOTAL-$PREV_TOTAL)-($IDLE-$PREV_IDLE))/($TOTAL-$PREV_TOTAL))/10)" | bc -l );

52.45

As you can see command output shows CPU is loaded on 52.45%, so this server will soon have to be replaced with better hardware, because it gets CPU loaded over 50%

It is useful to use above bash shell command one liner together with little for loop to refresh output every few seconds and see how the CPU is loaded in percentage over time.

 

for i in $(seq 0 10); do :;sleep=1;CPU=(`cat /proc/stat | head -n 1`);PREV_TOTAL=0;for VALUE in "${CPU[@]}”; do let “PREV_TOTAL=$PREV_TOTAL+$VALUE”;done;PREV_IDLE=${CPU[4]};sleep $sleep; CPU=(`cat /proc/stat | head -n 1`);unset CPU[0];IDLE=${CPU[4]};TOTAL=0; for VALUE in “${CPU[@]}"; do let "TOTAL=$TOTAL+$VALUE"; done;echo $(echo "scale=2; ((($sleep*1000)*(($TOTAL-$PREV_TOTAL)-($IDLE-$PREV_IDLE))/($TOTAL-$PREV_TOTAL))/10)" | bc -l ); done

47.50

13.86
27.36
82.67
77.18

To monitor "forever" output from all server processor overall load use:
 

while [ 1 ]; do :;sleep=1;CPU=(`cat /proc/stat | head -n 1`);PREV_TOTAL=0;for VALUE in “${CPU[@]}”; do let “PREV_TOTAL=$PREV_TOTAL+$VALUE”;done;PREV_IDLE=${CPU[4]};sleep $sleep; CPU=(`cat /proc/stat | head -n 1`);unset CPU[0];IDLE=${CPU[4]};TOTAL=0; for VALUE in “${CPU[@]}"; do let "TOTAL=$TOTAL+$VALUE"; done;echo $(echo "scale=2; ((($sleep*1000)*(($TOTAL-$PREV_TOTAL)-($IDLE-$PREV_IDLE))/($TOTAL-$PREV_TOTAL))/10)" | bc -l ); done

 

 

Share this on:

Download PDFDownload PDF

Tags: , , , , , , , , , ,

4 Responses to “Command to get CPU server load in % percentage using bash and /proc/stat on Linux”

  1. 360Safe Explorer 360Safe Explorer Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 QIHU 360SE

    Oh my goodness! Incredible article dude! Thank you so much, However I am having problems with your RSS.
    I don’t undersetand the reason whhy I am unable to join it.
    Is there anyone else having the same RSS problems?
    Anybody who knows the answer can you kindly respond?
    Thanx!!
    http://www.wiki.lavoxpopuli.com recently posted…http://www.wiki.lavoxpopuli.comMy Profile

    View CommentView Comment
  2. Yandex Browser 18.2.1.174 Yandex Browser 18.2.1.174 Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 YaBrowser/18.2.1.174 Yowser/2.5 Safari/537.36

    I am extremely impressed together with your writing talents as neayly
    as wirh the format on your blog. Is this a paid subject matter or did you modify it yourself?

    Either way keep up the nice high quality writing, it is uncommon to look a
    nice blog like this one nowadays..
    Norberto recently posted…NorbertoMy Profile

    View CommentView Comment
  3. Google Chrome 66.0.3359.117 Google Chrome 66.0.3359.117 Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36

    Terrific article! That iss the kind of information that
    should be shared across the internet.Disgrace on the search engines for no
    longer positioning this submit higher! Come on over and seek advice
    from myy site . Thanks =)
    https://www.latte.fm/ recently posted…https://www.latte.fm/My Profile

    View CommentView Comment
  4. gamingsays:
    Google Chrome 64.0.3282.39 Google Chrome 64.0.3282.39 Windows 10 x64 Edition Windows 10 x64 Edition
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36

    Greaqt web site. A lot of useful information here.
    I’m sending it to several buddies ans also sharing in delicious.

    And naturally, thabks on your effort!
    gaming recently posted…gamingMy Profile

    View CommentView Comment

Leave a Reply

CommentLuv badge