How to get the information about cpu idle from top command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the information about cpu idle from top command?
# 1  
Old 12-01-2009
Question How to get the information about cpu idle from top command?

I am using Ubuntu 9.04. I want to write a shell script to get the information about cpu idle from top command at the real time when i call it, compare cpu idle with 20 (20%), if cpu idle > 20 exit 1, vice versa exit 0. Anybody can help me to resolve it ?
Thanks alot.
# 2  
Old 12-01-2009
Have a look here
# 3  
Old 12-01-2009
CPU idle from the top command

Please, if you do not mind my asking, WHY are you trying to parse the CPU idle from the top command? There are better/easier ways to get the cpu idle figure. IS there some contraint that you MUST use the figure form top?
# 4  
Old 12-01-2009
Code:
#!/bin/bash
a=20
CPUIDLE=$(top -d 0.1 -n 2 | grep '^Cpu(s):'| tail -1 |awk '{print $5}'|sed 's/%.*//')
echo $CPUIDLE
if [ $CPUIDLE -lt $a ]
then
     exit 1
else
     exit 0
fi


When I run, it have error:
Code:
root@ubuntu:~# ./phan6.sh
88.5
./phan6.sh: line 5: [: 88.5: integer expression expected

Please help me to fix it.
Thanks in advanced
# 5  
Old 12-01-2009
Code:
if [ "$CPUIDLE" -lt "$a" ]

# 6  
Old 12-01-2009
The result still have error:
Code:
root@ubuntu:~# ./phan6.sh
88.5
./phan6.sh: line 5: [: 88.5: integer expression expected

Please check it for me.
Thank you very much.
# 7  
Old 12-03-2009
Code:
mpstat | tail -1 | cut -d' ' -f35



---------- Post updated 12-03-09 at 04:31 AM ---------- Previous update was 12-02-09 at 03:28 PM ----------

Code:
#! /usr/bin/env perl

my $cpu = qx~mpstat | tail -1 | perl -ane 'print $F[10]'~;
exit 1 if ( 100 - $cpu > 20 );

There is the code exiting "1" if you are above 20% CPU idle time accross all processor cores, average.

---------- Post updated at 04:58 AM ---------- Previous update was at 04:31 AM ----------

Code:
#! /bin/bash

CPU=$(mpstat | tail -1 | awk '{print $11}' | cut -c1-2);
if [ $CPU -gt 20  ]
then
    exit 1
fi

If you must have bash, instead of Perl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Idle Process Exhausting CPU

I noticed when having some trouble with code I was testing that the CPU was becoming exhausted and I would have to reboot. After rebooting a couple times I decided to check for other problems before trying my code again. That's when I noticed that the CPU with the idle process was through the roof:... (5 Replies)
Discussion started by: Azrael
5 Replies

2. UNIX for Dummies Questions & Answers

Query related to swap information shown by top command

Hi I have checked the output of top command in which there is a difference shown between the swap of top command for a process with total swap memory usage of the top command. Swap usage of process is higher than the total swap memory usage. top - 18:28:21 up 7:13, 5 users, load... (2 Replies)
Discussion started by: gagan2914
2 Replies

3. Solaris

top is showing 0% cpu Idle

What should we do if we show a 0% cpu idl on top? (5 Replies)
Discussion started by: Pouchie1
5 Replies

4. Shell Programming and Scripting

Command to find the Memory and CPU utilization using 'top' command

Hi all, I found like top command could be used to find the Memory and CPU utilization. But i want to know how to find the Memory and CPU utilization for a particular user using top command. Thanks in advance. Thanks, Ananthi.U (2 Replies)
Discussion started by: ananthi_ku
2 Replies

5. AIX

Need a list of top 10 CPU using processes (also top 10 memory hogs, separately)

Okay, I am trying to come up with a multi-platform script to report top ten CPU and memory hog processes, which will be run by our enterprise monitoring application as an auto-action item when the CPU and Memory utilization gets reported as higher than a certain threshold I use top on other... (5 Replies)
Discussion started by: thenomad
5 Replies

6. UNIX for Dummies Questions & Answers

Unix Top Command and sorting by CPU Usage

Ok, so I am using the Top command on my linux VPS to try and see the processes using the most CPU %. I hit the P to sort by CPU % but it wants to sort them from lowest to highest (ascending). My Telnet-SSH screen is only about 60 rows high so the processes with the highest CPU % usage are at the... (6 Replies)
Discussion started by: davemehta
6 Replies

7. UNIX for Dummies Questions & Answers

top command + %CPU usage exceeds 100%?

Hi there. I was looking at the output from running top and for short amounts of time, when I see all the process running and add up the values in the %CPU column the value exceeds 100% (I just add them quickly in my head). I assume that if I were to add up all my processes in the entire list,... (2 Replies)
Discussion started by: Carl1976
2 Replies

8. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux ..to use these values in shell script to kill processes exceeding cpu utilization.ps (pcpu) command does not give exact values..top does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

9. HP-UX

sar output gives 98% idle CPU

Dear All, Our HPUX 8 GB 8CPU database server is behaving abnormally for the last 4+ weeks. I have generated a sar output and it is here- 11:46:52 %usr %sys %wio %idle 11:46:53 1 1 6 92 11:46:54 0 1 0 99 11:46:55 0 1 0... (3 Replies)
Discussion started by: Ashrunil
3 Replies

10. Solaris

CPU idle

hi when should we consider that CPU is loaded? When it is 100% idle or 0%idle?? tx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies
Login or Register to Ask a Question