CPU usage reporting in terminal - macos

I am trying to get the CPU usage of a mac over time.
I am using this top cmd in terminal getting the result i want but would like it to output to a file and update every 5 seconds.
top -l 1 | grep -E "^CPU|^Phys"
CPU usage: 3.27% user, 14.75% sys, 81.96% idle
PhysMem: 5807M used (1458M wired), 10G unused.

This command prints all 3 CPU usage percentages tab-separated to a file (appending line by line for each call):
top -l1 | grep -E "CPU usage:" | awk -v FS="CPU usage: | user, | sys, | idle" '{print $2, $3, $4}' >> cpu_user_sys_idle.tsv
Works with pipe separated command chain:
Top as you suggested
Grep to filter only line with CPU usage
Awk with variable field-separator (-v FS) using either of the 4 strings to get all percentages as isolated fields. Then print second, third and fourth (omit first since it is empty).
>> redirects output appending to file (e.g. cpu_user_sys_idle.tsv)
You additionally can put it into automated or scheduled (apple)script to collect measures in regular intervals.

Related

How to get CPU usage from "top" and save it to file - MAC OS bash

I am using the terminal in MAC OS. I want to get on only the total ideal CPU from the top command (just ideal cpu) and stor it to a file.
I have tried this:
top | grep -w "CPU usage: "
And i got this :
/Test/temp1 » top | grep "CPU usage: " 130 ↵
CPU usage: 5.45% user, 11.77% sys, 82.77% idle
CPU usage: 3.20% user, 3.51% sys, 93.27% idle
CPU usage: 2.64% user, 2.76% sys, 94.59% idle
CPU usage: 3.18% user, 2.61% sys, 94.19% idle
.
I want to store only the last metrics en every line which is "82.77% idle" but without the "% and idel" word, only I want the amount of ideal CPU to store it in a file.
Ex:
82.77
93.27
94.59
94.19
I appreciate your help ,,,
You can use a combination of grep and awk to get the required number alone. Here the --line-buffered flag is used, which forces grep to write to the output each time a new line is encountered.
top | grep --line-buffered "CPU usage: " |awk '{ print substr($5, 1, length($5)-1) }'
Or you could directly use awk
top | awk '/^CPU usage: / { print substr($5, 1, length($5)-1) }'

Linux CPU and memory percentages

Is there a command which outputs just current CPU usage percentage and current memory usage percentage? As a single number, so no tables or formatted output.
The reason I'm asking. For my panel in XFCE I'd like to see something like this:
CPU 34% | MEM 56%
I haven't found a plugin which does that, so I aim to use the Generic Monitor plugin and give it a command which it should print and let it update every 1 sec.
Put the following snippet somewhere in a script:
#!/bin/bash
CPU=$(lscpu | grep '\(CPU\|max\) MHz:' | xargs echo | awk '{printf "%3.0f\n", $3*100/$7}')
MEM=$(free | grep Mem | awk '{printf "%3.0f\n", $3*100/$2}')
echo CPU $CPU% \| MEM $MEM%
And call it from genmon as bash /path/to/this/script.sh.

Get total CPU usage from the terminal on a Mac? [duplicate]

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
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.
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)
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.
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/\%/\/
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
You can do this.
printf "$(ps axo %cpu | awk '{ sum+=$1 } END { printf "%.1f\n", sum }' | tail -n 1),"

How to get CPU utilization in % in terminal (mac)

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
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.
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)
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.
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/\%/\/
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
You can do this.
printf "$(ps axo %cpu | awk '{ sum+=$1 } END { printf "%.1f\n", sum }' | tail -n 1),"

KSH Script to get the CPU usage

Can somebody please help me to write a KSH Script to get the CPU usage of the AIX server ?
Here I want my script to get the Current usage of CPU that time it is executed
There are a number of tools on AIX (and elsewhere) to get the current CPU usage.
nmon
On AIX (and Linux) you have nmon. This gives very detailed infos on memory, cpu usage, disk usage, etc. It is normally used as an interactive tool.
sar
call sar -u 1 1 to get the current cpu usage. See the manual page of sar for a whole lot of options. Depending on your installation you need to be root or add your user to the group "adm".
Just call w -u. It outputs a little bit more than you ask for. If you don't need that you can use awk/sed/cut to cut it away.
I use the following script in bash, but I just tried it in ksh and it works all the same:
top -bn2 | grep 'Cpu(s)' | sed -n '2s/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}
You can also use
top -bn1 | grep 'Cpu(s)' | sed -n 's/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}'
for faster response, but the result will be less accurate.

Resources