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}' 



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.


6 Answers 6


If you can afford a delay of one second, this will print CPU usage as a simple percentage:

echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')]

(Without the one-second delay, vmstat can only print average values since boot.)

  • Doesn't give me an accurate % (comparing with htop)
    – lepe
    Jul 14, 2015 at 8:08
  • 3
    You need to use vmstat 1 2 like this: echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')] Otherwise your vmstat is going to give values since boot which is likely not what you're after.
    – Joe
    Dec 5, 2015 at 23:21
  • joe thank you it works. please Paul, edit the answer so that everyone can see the correct command line Feb 2, 2017 at 6:22
  • Thanks for catching that, Joe and Pavlos! I didn't notice that part of the man page.
    – Paul
    Feb 2, 2017 at 16:28

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
  • Every other method including sar, mpstat... etc shows a 2/3% whereas this command always shows 50%. Jul 19, 2014 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, 2014 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. Jul 19, 2014 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, 2014 at 13:19
  • I have only 1 CPU. Jul 19, 2014 at 13:20

I have tried several ways, but this seems to me the most accurate:

cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'

Got it from here

  • my linux (Linux machine_name 4.4.0-127-generic #153-Ubuntu SMP Sat May 19 10:58:46 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) does not have column $15 (cpu line of the /proc/stat file): cpu 17411187 27478 594887 646652526 585551 0 31838 0 0 0
    – Kemin Zhou
    Jun 11, 2018 at 18:30
  • 2
    Best answer! Thanks! My variation: cat <(grep 'cpu ' /proc/stat) <(sleep 0.1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{printf "%.1f", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'
    – maXp
    Dec 3, 2019 at 22:25
  • maXp difference is that its printed rounded with a single decimal: %.1f, which in my opinion is more human readable and useful if you use that output in other scripts.
    – lepe
    Dec 4, 2019 at 9:04
  • thanks, this helped me. how to put this behind watch to be able to get overall cpu usage updated every couple seconds? Dec 25, 2021 at 17:43
  • @NaveenReddyMarthala : If you can create a script, its easier. I was unable to make it work with watch, but I guess its possible.
    – lepe
    Dec 27, 2021 at 5:47

I needed CPU usage per core and used mpstat from the sysstat package.

sudo apt install sysstat

CPU0 Utilization in %:

echo 100 - $(mpstat -P 0 | tail -1 | awk '{print $13}') | bc

CPU1 Utilization in %:

echo 100 - $(mpstat -P 1 | tail -1 | awk '{print $13}') | bc

Change the -P X if you have more than 2 cores.


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.


With a very small delay (500 ms approx), the current CPU usage in % can be found out using the following command

awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 0.5;grep 'cpu ' /proc/stat)

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.