LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH Script to get cpu idle % (https://www.linuxquestions.org/questions/programming-9/bash-script-to-get-cpu-idle-608970/)

pgb205 12-24-2007 07:52 PM

BASH Script to get cpu idle %
 
Here is the part of the larger script; I'm trying to get cpu idle time
using top command, then grepping for one of the top lines, the one with
cpu idle statistic, then field 4 with gawk and then getting rid of the percentage sign, blanks spaces etc (tr). This gets me the string I want ok but it seems that the string array has many blank elements.
So for example after i execute first line below and assuming cpu idle time is 90% i would get cpu_free equal to "90". However when i try to loop using for loop below i get many more elements than just two i should have. The 0 lement is "90" however. Any idea what I'm doing wrong here?


cpu_free=`top -n 1 | grep -i cpu\(s\)| gawk -F, '{print $4}' | tr -d "%,id. "`
echo $cpu_free
for ((i=0;i<=35;i++))
do
echo "element $i is |${cpu_free[$i]}|"
done

twantrd 12-24-2007 11:57 PM

I'm not sure why you are using a for loop here. Did you mean you wanted a while loop instead?

Also, your script doesn't work correctly because it also strips out the decimal. If my CPU was idling at 98.9%, your script would output it to 989.

Is this something that you had in mind?

Code:

while true; do top -n 1 | grep -i cpu\(s\) | gawk -F, '{print $4}' | sed 's/%.*//'; done
-twantrd

ghostdog74 12-25-2007 12:21 AM

Quote:

Originally Posted by twantrd (Post 3000949)
Code:

while true; do top -n 1 | grep -i cpu\(s\) | gawk -F, '{print $4}' | sed 's/%.*//'; done
-twantrd

shorter version
Code:

while ...top -n 1 | awk -F, '/Cpu\(s\)/{sub(/%.*/,"",$4);print $4}' ..done
the various options of top command may (or may not) indicate a way to only show the Cpu(s) line and field $4, so OP can read up on that if (s)he wants to.

pgb205 12-25-2007 10:41 AM

the only reason i used a for loop is to actually check that i have those extra array
elements. when i do string length test it shows that string is 36 characters long
even though only 2 numbers show up when i print it. That's why i constructed the for loop
to verify that there are actually those empty elements there.
I know that decimal point is removed I will fix this later for now i need to figure out
the problem above.
thanks

twantrd 12-25-2007 04:13 PM

Why check for the string length when you are displaying output from a certain field already?? That field remains constant and won't change. I don't understand your reasoning for a for loop in this one.

-twantrd

pgb205 12-25-2007 10:34 PM

the reason for me trying a loop is just to see if the string length command actually
output the correct answer. And it does. Line 1 in the script produces string array which
is 36 elements long. for loop confirms it. I'm not sure how to get rid of the empty
elements.

Basically how would you guys go about getting idle cpu percentage. Maybe I'm going about this in
a completely wrong way.


All times are GMT -5. The time now is 02:39 PM.