- Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcpu-utilization-macos.sh
31 lines (25 loc) · 927 Bytes
/
cpu-utilization-macos.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# By ChatGPT and masterfully adapted by Arne Tarara. No rights reserved :)
iostat -w 1 -n 0 | awk -v date_cmd="gdate +%s%N"'
NR > 3 { # skips first 3 rows, which contain header data and first average-only measurement
# Extract user, system, and idle CPU percentages
user = $1
sys = $2
idle = $3
# Calculate total CPU usage
usage = 100 - idle
# Get the current time in seconds with microseconds
cmd = date_cmd
cmd | getline current_time_ns
close(cmd)
# Calculate the time difference
if (last_time_ns != "") {
time_diff_ns = current_time_ns - last_time_ns
} else {
time_diff_ns = 1000000000 # No difference for the first line
}
# Print the time and CPU usage
printf "%.9f %.2f\n", (time_diff_ns / 1000000000), usage
# Store the current time as the last time for the next iteration
last_time_ns = current_time_ns
}'