0

I have to display the CPU usage on my application and update it in real time. I am using following command to get the CPU usage:

top -b -n 2 | grep Cpu | awk '{printf "CPU Load:%.2f\n", $(NF-13) + $(NF-15)}' | sed -n '2 p'

The above command does give me the required result but after sometime it stops working and i do not get the updated CPU usage. Any help that why command stops working after sometime.

  • What fields are you trying to parse – bac0n May 4 at 13:14
  • 1
    @bac0n ; he is trying to "add "us" and "sy" values and display it." he seems to have solved the add part, that was the previous issue. – Doug Smythies May 4 at 14:23
  • Show us examples of it working and not working. Define "after sometime" a few minutes, a few days, ...? – Doug Smythies May 4 at 14:43
  • @doug Smythies, its stops after about 4-5 minutes. – Waleed Naveed May 4 at 16:51
  • Show us an example of it working and another example of it not working. I can not get it not to. (by the way, what you are doing makes no sense to me at all and myself I think there are better ways.) – Doug Smythies May 4 at 18:08
0

You are counting fields backwards from the last one, assuming the number is always the same. However that is not true. One example, there may be more:

doug@s18:~$ top -b -n 2 | grep Cpu
%Cpu(s): 98.9 us,  1.1 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu(s):100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

Notice in the 2nd printing there are only 16 fields instead of 17, because the normal space between the : and the first digit of the "us" load isn't there having been taken up by the "1", and therefore the referencing would be wrong.

Looking in more detail, I'll add the field number below (remember, the default field delimiters are space and tab):

%Cpu(s): 98.9 us,  1.1 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
1        2    3    4   5    6   7    8   9    10  11   12  13   14  15   16  17
.
%Cpu(s):100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
1             2    3   4    5   6    7   8    9   10   11  12   13  14   15  16

Indeed, the output running your command gives an incorrect answer sometimes (running it in a loop,where the real load is always 100%):

CPU Load: 100.00
CPU Load:   0.00
CPU Load:   0.00
CPU Load: 100.00
CPU Load:   0.00
CPU Load:   0.00

This hack seems to fix this issue:

top -b -n 2 | grep Cpu | sed 's/:/ /g' | awk '{printf "CPU Load:%7.2f\n", $(NF-13) + $(NF-15)}' | sed -n '2 p'

I do not know if this was your issue or not, nor if it is the only issue.

| improve this answer | |
  • How the fields are different in both printings ? i could not get this, can you please elaborate it a bit more @Doug Smythies . . . . Yes, this is my issue, the result stops to update . . My other issue is that, i want to sync my application and top command result, currently the top command updates after 5 second but my application updates after 15-20 second. Any help on this ? – Waleed Naveed May 4 at 19:11
  • @WaleedNaveed : I edited my answer. Ask a new question for your sync issue. Myself I use time to know that data for a specific app. Bad example: time sleep 10 gives: real 0m10.001s user 0m0.000s sys 0m0.001s – Doug Smythies May 4 at 20:19
0

Calculate CPU usage based on Linux CPU utilization:

#!/bin/bash

declare -i total idle

# man: proc(5)
# Read first line, skip `cpu', guest, guest_nice.
cpu_usage(){
    local -a a
    local -n b=$1 c=total d=idle
    local    e
    local -i f g

    mapfile -t -s 2 -n 8 -d \  a < /proc/stat
    e=${a[*]}
    ((b=(((f=(g=${e// /+})-c)-(a[3]-d))*1000/f+5)/10))
    c=${g}
    d=${a[3]}
}

# Hide cursor and bring it back on exit.
trap 'tput cnorm' EXIT; tput civis

# An example how you can use cpu_usage
# in a while loop. cpu_usage will use the
# supplied argument as a name reference.
while true; do
    cpu_usage load
    printf \\r%d%%\ \\b $load
    sleep 0.2
done
| improve this answer | |

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.