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 10-20-2005, 05:28 PM   #1
happy78
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
getting total CPU's idle


Hi guys,

I'm having problem retrieving the the correct total cpu's ide with this command: top -n 1.

Sometimes, I'm able to retrieve the total cpu's idle of my machine by doing this:
cpus=($(top -b -n 1))
echo ${cpus[39]} #>> /tmp/$1


BASED on this output of "top -n 1":

15:22:56 up 86 days, 9:04, 11 users, load average: 0.06, 0.05, 0.01
127 processes: 125 sleeping, 1 running, 1 zombie, 0 stopped
CPU states: cpu user nice system irq softirq iowait idle
total 0.2% 0.0% 0.2% 0.0% 0.0% 0.0% 99.5%
cpu00 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu01 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu02 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu03 0.9% 0.0% 0.9% 0.0% 0.0% 0.0% 98.0%


So, at index cpus[39] the idle value is: 99.5%
That's correct.
But once in awhile the out put of "top -n 1" returns different output:

15:12:35 up 52 min, 3 users, load average: 0.29, 0.17, 0.09
83 processes: 81 sleeping, 1 running, 1 zombie, 0 stopped
CPU states: cpu user nice system irq softirq iowait idle
total 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu00 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu01 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu02 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
cpu03 0.0% 0.0% 0.0% 0.0% 0.0% 0.0% 100.0%
This output is missing Time like the one. So, that shifted the position of cpu idle. Therefore, at cpus[39], the value is cpu00, instead of 100.0%

[/B]Is there any command that can retrieve cpu's total idle time better than "top -n 1"[/B]
 
Old 10-20-2005, 07:38 PM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
You could skip to the relevant line, e.g.,
Code:
cpus=`top -n 1 | tail +3`
or
Code:
cpus=`top -n 1 | grep ^total`
Then it's more reliably the same element (and easier to count!). My "top" output differs, but yours should be $cpus[8]. You can use $(...) instead of `...`.
 
Old 10-20-2005, 08:25 PM   #3
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Hi
thank you for your response.
That helps alot, and I also have other solution:

let freeMB=`free -m | tail -2 | head -1 | awk '{print $4}'`
cpus=`top -n 1| head -4 | tail -1 | awk '{print $8}'`

echo $freeMB
echo ${cpus}


But I have a java program waiting to catch freeMB's and cpus' value. However, only freeMB's value is captured, and cpus' value isn't.
is there a problem with stdin?
 
Old 10-20-2005, 09:22 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Try that one (even though yours SHOULD work, too):
Code:
top -n 1 | awk '/cpu/ && $8 ~ "[0-9]" {print $8}'


Cheers,
Tink
 
Old 10-20-2005, 09:25 PM   #5
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
thank you for replying..
yes it works..
but now i run into a problem with java and linux...
java is waiting for my script to echo back with cpu's and freeMem's value....however, only freeMem's is caught..
looks like tty is lost..any suggestion?
 
Old 10-20-2005, 09:45 PM   #6
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
It's ok I fixed the problem by adding -b infront of "top"
 
  


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
no of CPU's in a Linux server palanisaravanan Linux - Hardware 2 11-11-2005 04:34 AM
Stealth cpu's EdR Linux - Newbie 4 10-14-2004 01:35 PM
CPU's doralsoral Linux - Hardware 2 10-29-2003 05:27 AM
CPU's on Linux zakl Linux - Hardware 1 06-12-2003 02:54 AM
two processes on different CPU's CobraMagic Linux - General 1 11-08-2002 11:34 AM

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

All times are GMT -5. The time now is 10:29 AM.

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