On Thursday 09 August 2007 14:22, Adam Thornton wrote:
>If you want to print the percentage of non-idle CPU:
>
>CPU=$((100 - $(vmstat | tail -1 | awk '{print $15}'))); echo $CPU
>
>(assuming your vmstat produces the same output as mine).
Actually, that shows the average CPU usage since last reboot, because vmstat's
default output contains the average. If you want to get the current CPU
usage, you have to get two lines of output and it will give you the average
over the time between those two lines. Thus "vmstat 1 2" will show the
average over one second, and you can do this to get just the number you want:
vmstat 1 2 | awk '{idle=$15} END {print 100 - idle}'
(Note that I'm doing all the work Adam did with tail and the shell in awk
here, because I like awk. All its doing is collecting the 15th field from
each line, and after the input ends its got the idle CPU value from the last
line, which is what we need.)
By changing that first argument to vmstat, you can get the average CPU usage
over any number of seconds.
- MacK.
-----
Edmund R. MacKenty
Software Architect
Rocket Software, Inc.
Newton, MA USA
----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390