50

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
Joel
  • 11,431
  • 17
  • 62
  • 72
  • 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 '09 at 07:23
  • I'm aware of this. In fact I comment on an answer to that effect. – Joel Aug 26 '09 at 07:25
  • 5
    How is this off topic? Duplicate I can understand, but off topic? – puk Sep 30 '18 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 02 '20 at 09:25

7 Answers7

44

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • 1
    If this is true, then why does Space's answer work? – Brain2000 Nov 13 '13 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 '16 at 11:11
26

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.

Space
  • 7,049
  • 6
  • 49
  • 68
  • 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. – Dénes Tarján Aug 28 '09 at 11:51
  • Would increasing the number of iterations with -n > 1 be more precise? Have you tested this case? – Christian May 04 '12 at 06:45
  • 6
    top's calculation of %CPU is different from ps's calc of %CPU. see this answer http://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result – BozoJoe Sep 23 '13 at 21:59
  • top -b -n 2|grep Cpu|tail -n 1|cut -d ' ' -f 7|cut -d '%' -f 1 – CpnCrunch Apr 01 '20 at 23:38
12

Try this command:

$ top

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

aolde
  • 2,287
  • 16
  • 19
  • 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 '09 at 07:19
  • 1
    @Joel: you can use the top command to read the cpu values and exit. use -b with top. – Space Aug 26 '09 at 07:40
12

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
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/linuxkernelnewbies@googlegroups.com/msg01690.html http://www.linuxhowtos.org/System/procstat.htm

Dénes Tarján
  • 576
  • 2
  • 4
  • 16
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
Johan
  • 20,067
  • 28
  • 92
  • 110
  • 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)}'`. – Dušan Brejka Jul 30 '19 at 09: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.

elcuco
  • 8,948
  • 9
  • 47
  • 69
  • 1
    Thanks - but there is no way to calculate CPU utilization from the load average. – Joel Aug 26 '09 at 07: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 '09 at 09:00