52

How can I determine the current CPU utilization from the shell in Linux?

For example, I get the load average like so:

cat /proc/loadavg

Outputs:

0.18 0.48 0.46 4/234 30719
4
  • Just FYI, the load average is not the CPU usage although it may indirectly show the CPU usage. It's a number showing how many processes are out there running but waiting for resources, be it CPU or disk or something else.
    – hhaamu
    Aug 26, 2009 at 7:23
  • I'm aware of this. In fact I comment on an answer to that effect.
    – Joel
    Aug 26, 2009 at 7:25
  • 6
    How is this off topic? Duplicate I can understand, but off topic?
    – puk
    Sep 30, 2018 at 17:09
  • 1
    can't answer the question cause it's closed but have a look at vmstat, as a bonus it works on BSDs as well.
    – minusf
    Oct 2, 2020 at 9:25

7 Answers 7

47

Linux does not have any system variables that give the current CPU utilization. Instead, you have to read /proc/stat several times: each column in the cpu(n) lines gives the total CPU time, and you have to take subsequent readings of it to get percentages. See this document to find out what the various columns mean.

2
  • 1
    If this is true, then why does Space's answer work?
    – Brain2000
    Nov 13, 2013 at 17:49
  • 12
    Because Space's answer doesn't work. It returns the average CPU usage since process start and not the current CPU usage. This answer is the correct one.
    – scai
    Aug 12, 2016 at 11:11
30

You can use top or ps commands to check the CPU usage.

using top : This will show you the cpu stats

top -b -n 1 |grep ^Cpu

using ps: This will show you the % cpu usage for each process.

ps -eo pcpu,pid,user,args | sort -r -k1 | less

Also, you can write a small script in bash or perl to read /proc/stat and calculate the CPU usage.

4
  • 2
    We tried, and "top -b -n 1" gives very inaccurate results. "top"'s first iteration is very approximative, can give 10 percent instead of 60. Aug 28, 2009 at 11:51
  • Would increasing the number of iterations with -n > 1 be more precise? Have you tested this case?
    – Christian
    May 4, 2012 at 6:45
  • 7
    top's calculation of %CPU is different from ps's calc of %CPU. see this answer unix.stackexchange.com/questions/58539/…
    – BozoJoe
    Sep 23, 2013 at 21:59
  • top -b -n 2|grep Cpu|tail -n 1|cut -d ' ' -f 7|cut -d '%' -f 1
    – CpnCrunch
    Apr 1, 2020 at 23:38
13

Try this command:

$ top

http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html

2
  • 1
    Unfortunately I need it to exit right away so I can parse the output. I'm hoping there is a file in /proc that I can read.
    – Joel
    Aug 26, 2009 at 7:19
  • 1
    @Joel: you can use the top command to read the cpu values and exit. use -b with top.
    – Space
    Aug 26, 2009 at 7:40
13

The command uptime gives you load averages for the past 1, 5, and 15 minutes.

7

Try this command:

cat /proc/stat

This will be something like this:

cpu  55366 271 17283 75381807 22953 13468 94542 0
cpu0 3374 0 2187 9462432 1393 2 665 0
cpu1 2074 12 1314 9459589 841 2 43 0
cpu2 1664 0 1109 9447191 666 1 571 0
cpu3 864 0 716 9429250 387 2 118 0
cpu4 27667 110 5553 9358851 13900 2598 21784 0
cpu5 16625 146 2861 9388654 4556 4026 24979 0
cpu6 1790 0 1836 9436782 480 3307 19623 0
cpu7 1306 0 1702 9399053 726 3529 26756 0
intr 4421041070 559 10 0 4 5 0 0 0 26 0 0 0 111 0 129692 0 0 0 0 0 95 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 369 91027 1580921706 1277926101 570026630 991666971 0 277768 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 8097121
btime 1251365089
processes 63692
procs_running 2
procs_blocked 0

More details:

http://www.mail-archive.com/[email protected]/msg01690.html http://www.linuxhowtos.org/System/procstat.htm

6

Maybe something like this

ps -eo pid,pcpu,comm

And if you like to parse and maybe only look at some processes.

#!/bin/sh
ps -eo pid,pcpu,comm | awk '{if ($2 > 4) print }' >> ~/ps_eo_test.txt
1
  • And if you'd like to sum CPU usage by process name (for example if you're running chrome that spawns dozens of processes with the same name), you can use ps -eo pcpu,comm | awk '{if ($1 > 0.1) arr[$2]+=$1} END {for (key in arr) printf("%s\t%s\n", arr[key], key)}'. Jul 30, 2019 at 9:57
-3

You need to sample the load average for several seconds and calculate the CPU utilization from that. If unsure what to you, get the sources of "top" and read it.

2
  • 1
    Thanks - but there is no way to calculate CPU utilization from the load average.
    – Joel
    Aug 26, 2009 at 7:21
  • Actually there is, again, read the source. I think, that the load average is the derivative of the CPU utilization, to you need to make some sort of integral to get the CPU utilization. No better way to explain this - just use the source Luck.
    – elcuco
    Aug 26, 2009 at 9:00

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