Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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.


share|improve this question
up vote 1 down vote accepted

If you want to improve your example use following egrep

$ echo $CPU_SUMMARY | egrep -o '([0-9\.]+%us)'

On the other hand this will sum all CPU usage (in 3rd column of ps output from example)

$ ps fuxw | awk '{ if ($3 ~ /^[0-9]/) {SUM +=$3}} END  {print SUM"%"}'      
share|improve this answer
Unfortunately when I run that command, I still get the following: $ echo $CPU_SUMMARY | egrep -o '([0-9\.]+%us)' returns 24ms, 0.8y, 0.0ubuntui, 97.77d, 0.2Tue 9a, 0.0143i, 0.0i, 0.2t | egrep -o '([0-9\.]+s)'1.1%us – Matthew O'Riordan Oct 9 '12 at 13:08
BTW. For your latter example, that seems to work quite well, however it appears to only be adding up the root processes? Surely one would need it to use all processes for the calculation? – Matthew O'Riordan Oct 9 '12 at 13:12
if you want all processes just add 'a' as in 'ps fuxwa' to ps command... egrep works on my system as shown in example; check which shell are you running – Hrvoje Špoljar Oct 9 '12 at 13:36
Using zsh, any reason why that would be a problem? – Matthew O'Riordan Oct 9 '12 at 13:54
BTW. With your latter example, the ps fuxw version, I deliberately set a process to 100% and ran the command, and the CPU utilisation for that process was low. It seems to be using an average instead of actual. Any idea how that can use actual instead? – Matthew O'Riordan Oct 9 '12 at 13:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.