67

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
3
  • 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. Commented Aug 26, 2009 at 7:23
  • 9
    How is this off topic? Duplicate I can understand, but off topic? Commented 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. Commented Oct 2, 2020 at 9:25

7 Answers 7

56

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If this is true, then why does Space's answer work?
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.
33

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.

10 Comments

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.
Would increasing the number of iterations with -n > 1 be more precise? Have you tested this case?
top's calculation of %CPU is different from ps's calc of %CPU. see this answer unix.stackexchange.com/questions/58539/…
LC_ALL=C top -b -n 1 |grep ^%Cpu gives CPU stats for me in Ubuntu.
@jarno this is the answer to "How can I determine the current CPU utilization from the shell" (assuming that means for the entire cpu(s)). The command gives the idle % and you can subtract from 100 if you want cpu utilization.
|
19

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

Comments

14

Try this command:

$ top

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

2 Comments

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: you can use the top command to read the cpu values and exit. use -b with top.
8

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

Comments

7

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

2 Comments

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šanBrejka You may have to add LC_ALL=C for awk, at least if it is mawk depending on your locale.
-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 Comments

Thanks - but there is no way to calculate CPU utilization from the load average.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.