Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When I execute following command to get cpu usage , I get nice + user cpu usage.

top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}' 

Output:

14.5

Here I am getting problem is that the output depends on top command thus it doesn't change instantly as top command. So I am not getting correct cpu instantly. It gives same output and not changing.

I want to get real-time cpu usage in output. Please help me to improve my command.

share|improve this question
    
real time monitoring askubuntu.com/questions/293426/… –  Qasim May 21 '13 at 0:57

3 Answers 3

I used this when I needed to get CPU usage as a simple percentage:

echo $[100-$(vmstat|tail -1|awk '{print $15}')]
share|improve this answer

This is a known issue with top. As explained here, the 1st iteration of top -b returns the percentages since boot, we therefore 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
share|improve this answer
    
Every other method including sar, mpstat... etc shows a 2/3% whereas this command always shows 50%. –  CMCDragonkai Jul 19 at 8:03
    
@CMCDragonkai the numbers should change (I just checked it) are you sure you copy/pasted it correctly? Also, note that on systems with multiple CPUs, top will report the % usage as a percentage of a single CPU. This means you will often get much higher percentages. See point 9 here. –  terdon Jul 19 at 13:08
    
I think this command might not be compatible with my system. But it's apparent that all the other methods I tried gets me a percentage of 1/2/3% which is what I see if I run htop, whereas this command gives me an anomaly. –  CMCDragonkai Jul 19 at 13:16
    
@CMCDragonkai that's because of the multiple CPUs. You can either get the number returned by this and divide by the number of cores or you can use top in interactive mode (just run top) and, once there, hit I (shift + i) to see a breakdown by cores. –  terdon Jul 19 at 13:19
    
I have only 1 CPU. –  CMCDragonkai Jul 19 at 13:20

Use -n2. This will output two lines. The 1st time top prints the line does not qualify for the state at that point in time. Then adjust your script to ignore the first line.

share|improve this answer

Your Answer

 
discard

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.