Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 03-22-2006, 02:33 AM   #1
Registered: Mar 2004
Posts: 40

Rep: Reputation: 15
script to get cpu usage idle %


Can anyone give me an idea of how to get the CPU idle time? Is using the output from "top" the only way?

I cannot afford to write the script to parse the ouput from "top", as I need to run the jobs in various machines with one, two and four processor nodes.

So, i need a script that can deliver cpu load in the form of %idle, so that i can submit the jobs to those nodes that are free.

Old 03-22-2006, 02:58 AM   #2
LQ Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,175
Blog Entries: 1

Rep: Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042Reputation: 2042
You can take a look at /proc/stat:
cat /proc/stat
For explanation of the various fileds:
man proc
Old 03-22-2006, 04:38 AM   #3
Registered: Mar 2006
Posts: 110

Rep: Reputation: 16
Re. script to get cpu usage idle %


the CPU idle % is actually (100 - used %).

for calculating the used % you can use

ps -eo "%C"

It will list all the processes's CPU usage in %. Now you can safely remove those with " 0.0% " or idle processes.

Count all the non-zero %s and subtract from 100.

Old 03-22-2006, 05:26 AM   #4
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Or use some more advanced monitoring tools like nagios or gkrellm.
They're probably graphical, but may have a command line interface too.
Old 03-22-2006, 07:41 PM   #5
Registered: Mar 2004
Posts: 40

Original Poster
Rep: Reputation: 15
I tried the script using ps... But, the point is, in multi-cpu machine, the idle time is shown as more than 100%. in a dual cpu, it is 200%, and for four cpu node, it is 400%. That is where I got stuck up
Old 03-22-2006, 08:06 PM   #6
Registered: Jan 2005
Location: MA
Distribution: Ubuntu 7.10
Posts: 558

Rep: Reputation: 31
gkrellm should be able to fix that. I havent tried it myself because i only have one processor but it should show you the load on each processor individually
Old 03-22-2006, 10:18 PM   #7
Registered: Mar 2004
Posts: 40

Original Poster
Rep: Reputation: 15
The nodes donot have the gkrellm and nagios... For now, the only option i have is to find the number of processors from a script and then use ps to find the cpu % and subtract it from {num_proc*100}...

But, isnt it surprising that there is no script to give u the idle time straight away? How does one perform load balancing if one doesnot know the idle % of a node?

Can it be found out in run time? I mean in a C code?

and, also, can anyone help me how to add a list of cpu % from an output of a ps command? I know it can be done with awk... but, am just learning shell programming... In short, I need a script that gives me CPU % as a number

Thanks guyz...
Old 03-23-2006, 01:34 AM   #8
Registered: Mar 2006
Posts: 110

Rep: Reputation: 16
ok I believe enough has been said about the idle time and using various tools. Being a Linux preson we should not be tempted to go for tools rather put your hands on some shell scripting

Here is the Linux matra for CPUs

command:- cat /proc/stat| grep cpu


#cpu (user system nice idle)---->processes
cpu 1347655 198648 156415 131616078
cpu0 408592 50247 49112 32821748
cpu1 291843 51332 33225 32953299
cpu2 336032 49001 39752 32904914
cpu3 311188 48068 34326 32936117

here the values are in jiffers (unsigned long int). The sum total and the max. value for a CPU is 33329699

that means
cpu0 408592 + 50247 + 49112 + 32821748 = 33329699

hence idle % for CPU 0 ==> (32821748/33329699)*100


dont go for readymade solutions when you can work with your own

Let me know if anyone has any questions about it.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
CPU idle binumvk Linux - Security 0 10-16-2004 04:19 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
High idle cpu load in 2.6.4? geekzen Linux - General 4 04-10-2004 11:54 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 05:55 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:15 AM.

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