LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2007, 07:52 PM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Rep: Reputation: 15
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
 
Old 12-24-2007, 11:57 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
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
 
Old 12-25-2007, 12:21 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by twantrd View Post
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.
 
Old 12-25-2007, 10:41 AM   #4
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Original Poster
Rep: Reputation: 15
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
 
Old 12-25-2007, 04:13 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
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
 
Old 12-25-2007, 10:34 PM   #6
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Original Poster
Rep: Reputation: 15
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.

Last edited by pgb205; 12-25-2007 at 11:13 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make CPU idle? asahlot Linux - Software 2 11-23-2006 05:21 AM
Cpu load at 59% when idle stubbe Linux - General 12 09-26-2006 01:15 PM
script to get cpu usage idle % LinToWinWin Linux - Software 7 03-23-2006 01:34 AM
CPU idle binumvk Linux - Security 0 10-16-2004 04:19 AM
High idle cpu load in 2.6.4? geekzen Linux - General 4 04-10-2004 11:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration