I am running top -b -n2 -d 1 | grep Cpu in a loop and notice it returns two entries in each iteration...

1) For each loop there are two lines of results...should I be using the first or second line...what is the difference between the two?

2) To calculate the CPU utilization do I add %us, %sy, %ni, %hi and %si?

Cpu(s):  1.6%us,  1.7%sy,  0.0%ni, 96.6%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Cpu(s):  8.7%us,  9.4%sy,  0.0%ni, 81.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Cpu(s):  1.6%us,  1.7%sy,  0.0%ni, 96.6%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Cpu(s):  9.7%us,  8.9%sy,  0.0%ni, 81.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
  • Take 100, remove %id. – Kusalananda Sep 22 '17 at 17:22
  • You have two lines because you've told top to run twice (-n2) before ending and passing the output to grep. – garethTheRed Sep 22 '17 at 17:23
  • Just as a heads up, you can use mpstat to achieve a similar output without having to parse top, if your distribution has it. – Thegs Sep 22 '17 at 19:13
  1. @garethTheRed is correct, you're asking for two iterations of output.

  2. That depends on what you mean by 'CPU utilization'. Each item on the line represents something different:

    • %us is the time spent in user mode by processes with a nice value of 0 or higher. This includes most of what most user applications do.
    • %sy is the time spent in kernel mode that doesn't fall under any other area. This is mostly time spent on system calls.
    • %ni is the time spent in user mode by processes with a nice value below 0. In essence, this is background tasks.
    • %id is the time spent doing nothing. It should equal 100 minus the sum of the other values.
    • %wa is time spent waiting on I/O completion that isn't spent doing something else. This includes time spent waiting to read or write data to the disk.
    • %hi is time spent in kernel mode servicing hardware interrupts. On most good systems, this should be near zero.
    • %si is time spent in kernel mode servicing software and deferred interrupts (on most systems, this is mostly network interrupts).
    • %st is time that the system could be running something, but another virtual machine was busy instead. This should be zero unless you're running virtual machines yourself, or are running on a cloud hosting platform like EC2, GCE, DigitalOcean, or Linode. This one may not show up on some systems, especially old or non-Linux systems.

By most non programmer's definitions, CPU utilization of a system is equal to the sum of %us, %sy, and %ni (and in fact, old UNIX systems only show these values). A more accurate statement would be that it's equal to the sum of everything except %id, %wa, and %st (because the CPU is quite literally doing nothing in those states).

Using the example lines you gave, the utilizations under the second definition would be: 3.4%, 18.1%, 3.4%, and 18.6%.

| improve this answer | |
 command = "top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'" 

The 1st iteration of top -b returns the percentages since boot. 
We need at least two iterations (-n 2) to get the current percentage. 
To speed things up, you can set the delay between iterations to 0.01. 
top splits CPU usage between user, system processes and nice processes, we want the sum of the three. 
Finally, you grep the line containing the CPU percentages and then use gawk to sum user, system and nice processes:

top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'
        -----  ------   -----------    ---------   ----------------------
        |      |           |             |             |------> add the values
        |      |           |             |--> keep only the 2nd iteration
        |      |           |----------------> keep only the CPU use lines
        |      |----------------------------> set the delay between runs
        |-----------------------------------> run twice in batch mode
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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