How to get CPU utilization in % in terminal (mac)
Asked Answered
P

7

26

Ive seen the same question asked on linux and windows but not mac (terminal). Can anyone tell me how to get the current processor utilization in %, so an example output would be 40%. Thanks

Prejudicial answered 15/6, 2015 at 21:42 Comment(4)
This works on a Mac ps -e -o %cpu | awk '{s+=$1} END {print s}'Vuong
thanks this is what i wanted, just got to add the % now! thanksPrejudicial
@vcsjones, post this as an answer so OP could accept itDippy
Why not launch Activity Monitor?Fasciate
V
51

This works on a Mac (includes the %):

ps -A -o %cpu | awk '{s+=$1} END {print s "%"}'

To break this down a bit:

ps is the process status tool. Most *nix like operating systems support it. There are a few flags we want to pass to it:

  • -A means all processes, not just the ones running as you.
  • -o lets us specify the output we want. In this case, it all we want to the cpu% column of ps's output.

This will get us a list of all of the processes cpu usage, like

0.0
1.3
27.0
0.0

We now need to add up this list to get a final number, so we pipe ps's output to awk. awk is a pretty powerful tool for parsing and operating on text. We just simply add up the numbers, then print out the result, and add a "%" on the end.

Vuong answered 15/6, 2015 at 22:47 Comment(5)
That method isn't very accurate since these values are rounded. The real CPU usage might be much bigger or much smaller, which can be checked with the Activity Monitor.Interlocution
I am using this command to get the % CPU usage on Mac, but I am getting certain percentage values which are greater than 100. Any idea why this could be?Datolite
This is super helpful because it works in the recovery mode terminal. Thanks. @Datolite You may see >100% because you have more than 1 core. Or because of rounding issues.Faubion
Also works over SSH, for checking on remote machines! @Questionnaire: chmac is right; you'll see the same thing if you sum up CPU utilization across processes in Activity Monitor; maximum utilization = (# cores * 100%).Stand
This can't be a valid answer -- the measurement of CPU usage is very inaccurate compared to the approaches using top -l or what is reported from the macOS activity monitor.Bedplate
P
8

Building on previous answers from @Jon R. and @Rounak D, the following line prints the sum of user and system values, with the added percent. I've have tested this value and I like that it roughly tracks well with the percentages shown in the macOS Activity Monitor.

top -l  2 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5"%" }'

You can then capture that value in a variable in script like this:

cpu_percent=$(top -l  2 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5"%" }')

PS: You might also be interested in the output of uptime, which shows system load.

Pianette answered 20/11, 2020 at 14:4 Comment(0)
L
7

Adding up all those CPU % can give a number > 100% (probably multiple cores).

Here's a simpler method, although it comes with some problems:

top -l 2 | grep -E "^CPU"

This gives 2 samples, the first of which is nonsense (because it calculates CPU load between samples).

Also, you need to use RegEx like (\d+\.\d*)% or some string functions to extract values, and add "user" and "sys" values to get the total.

(From How to get CPU utilisation, RAM utilisation in MAC from commandline)

Lithography answered 4/5, 2020 at 18:51 Comment(0)
C
4

Building upon @Jon R's answer, we can pick up the user CPU utilization through some simple pattern matching

top -l 1 | grep -E "^CPU" | grep -Eo '[^[:space:]]+%' | head -1

And if you want to get rid of the last % symbol as well,

top -l 1 | grep -E "^CPU" | grep -Eo '[^[:space:]]+%' | head -1 | sed s/\%/\/
Castellatus answered 9/7, 2020 at 19:6 Comment(0)
B
0

You can do this.

printf "$(ps axo %cpu | awk '{ sum+=$1 } END { printf "%.1f\n", sum }' | tail -n 1),"
Bradney answered 18/6, 2019 at 18:38 Comment(0)
Q
0
top -F -R -o cpu

-F Do not calculate statistics on shared libraries, also known as frameworks.

-R Do not traverse and report the memory object map for each process.

-o cpu Order by CPU usage

Answer Source

Quadricycle answered 17/3, 2021 at 14:0 Comment(0)
K
0

This only produces statistics, and skips extra work:

top -R -F -n 0

To find CPU utlliization, parse this as shown by Michael Behrens:

top -R -F -n 0 -l 2 -s 0 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5"%" }'

Extra -s 0 makes the second print follow with no delay (the default is 1 sec). Jon R has explained very well why one sample is not enough: it shows average CPU usage for an unspecified period of time.

Kesha answered 30/4, 2023 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.