1

I am trying to capture the CPU idle time (id) from top.

The following code captures Load Average and I am trying to manipulate the following code so that it capture's CPU idle time.

Any ideas welcome.

top -bn1 | grep load | awk '{printf "CPU load %: %.2f\n", $(NF-2)}' 

The above code outputs:

CPU load %: 0.44

I want to change the code so that it outputs CPU idle time

CPU Id %: 92.9%

Example Top output:

top - 10:35:25 up 1 day, 16:06,  5 users,  load average: 0.24, 0.16, 0.15
Tasks: 210 total,   2 running, 198 sleeping,  10 stopped,   0 zombie
%Cpu(s):  2.2 us,  0.2 sy,  4.7 ni, 92.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.1 st
KiB Mem:  16433064 total,  1353396 used, 15079668 free,   180944 buffers
KiB Swap:        0 total,        0 used,        0 free.   700468 cached Mem

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
24293 ubuntu    30  10   32828   2576   1608 S  19.3  0.0   0:25.30 fiberlamp
 2173 ubuntu    20   0   51200  16496   4952 S   9.3  0.1 263:34.18 Xvnc4
12648 ubuntu    20   0   23668   1732   1180 R   0.3  0.0   0:04.25 top 
...
U880D
  • 8,601
  • 6
  • 24
  • 40
Sascha
  • 398
  • 6
  • 14
  • 2
    What's the output you getting and what do you expect? – SMA Jul 14 '15 at 09:57
  • 1
    Try to look to [this question](http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux) – Alepac Jul 14 '15 at 09:59
  • The expected output is still unclear... – Karoly Horvath Jul 14 '15 at 10:52
  • I hesitate to give an answer because, just for example, *my* line #3 in top looks like this (note the absence of "id" for idle, and the use of decimal comma instead of decimal point): `%Cpu(s): 0,0 be, 0,0 sy, 0,0 ni, 99,9 un, 0,1 wa, 0,0 hi, 0,0 si, 0,0 st ` -- the exact output of `top` may vary (depending on platform, locale etc.), and your capture line might come up empty. – DevSolar Jul 14 '15 at 10:54
  • Thanks for this.- I need to get it consistent across applications. – Sascha Jul 14 '15 at 11:13
  • Expected Output -I'm trying to capture 92.9 id value from top. – Sascha Jul 14 '15 at 11:21

1 Answers1

1

grep for '%Cpu(s)'

top -bn1 | grep '%Cpu(s)' | awk -F',' '{printf "CPU id %: %.2f%\n", $4}'
RenBrian
  • 82
  • 2
  • 1
    Add `LANG=en_US` to avoid decimal comma issues. And I am still not sure if you can authoritatively rely on "idle" being the fourth value in the line... – DevSolar Jul 14 '15 at 11:40
  • Hi, I'm unclear what you mean by Add LANG=en_US where should I do that please? – Sascha Jul 16 '15 at 14:39
  • One thing with this solution is that the CPU metric seems to always return the same value. 94.5% However, when I run top in parallel I get CPU Idle with varying metrics – Sascha Jul 16 '15 at 15:20