I'm trying to do what I think is quite a simple task, yet I am hitting an odd problem, and unfortunately with my limited Bash experience (I generally use Ruby for scripts) I need some help.
All I am trying to do is write a script that simple checks CPU utilisation, and if utilisation is over a particular amount, it will do something.
So I started off by simply capturing CPU utilisation from
top as follows:
CPU_SUMMARY=(`top -b -n 1 | grep ^Cpu`)
Unfortunately, when I check what comes out of this, I get some extra information at the start:
$ echo $CPU_SUMMARY 24ms, 0.8y, 0.0ubuntui, 97.77d, 0.2Tue 9a, 0.0137i, 0.0i, 0.2tCpu(s): 1.1%us, 0.8%sy, 0.0%ni, 97.7%id, 0.2%wa, 0.0%hi, 0.0%si, 0.2%st
Where is the "24ms, 0.8y...0.2t" coming from? If I run the same command
top -b- n 1 | grep ^Cpu without trying to assign it to variable, I get:
Cpu(s): 1.1%us, 0.8%sy, 0.0%ni, 97.7%id, 0.2%wa, 0.0%hi, 0.0%si, 0.2%st
So I am just a bit confused as to why that initial 24ms... text is coming from.
I even tried extracting the bit I wanted out, ignoring the additional text as follows:
$ echo $CPU_SUMMARY | grep -o -E "[0-9\.]+%us" 24ms, 0.8y, 0.0ubuntui, 97.77d, 0.2Tue 9a, 0.0142i, 0.0i, 0.2t | grep -o -E "[0-9\.]+s"1.1%us
And I get the same thing?
I am un Ubuntu 12.04.
Thanks for your help.