-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb-cpu
executable file
·29 lines (23 loc) · 1.05 KB
/
sb-cpu
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
#!/bin/bash
sleep_duration_secs=0.3
compute_cpu_utilization() {
local stats_raw=$(grep -P 'cpu ' "/proc/stat")
local stats=($stats_raw) # array created from splitting the string on whitespaces
# echo "$stats_raw"
local user=${stats[1]}; local nice=${stats[2]}; local system=${stats[3]}
local idle=${stats[4]}; local iowait=${stats[5]}; local irq=${stats[6]}
local softirq=${stats[7]}; local steal=${stats[8]}
# echo "$user $nice $system $idle $iowait $irq $softirq $steal"
local idle_total=$((idle + iowait))
local non_idle=$((user + nice + system + irq + softirq + steal))
local total=$((idle_total + non_idle))
echo "$total" "$idle_total"
}
read ctotal cidle <<< $(compute_cpu_utilization)
sleep "$sleep_duration_secs"
read ptotal pidle <<< $(compute_cpu_utilization)
totald=$((ctotal - ptotal))
idled=$((cidle - pidle))
cpu=$(awk "BEGIN {print int(0.5 + 100*100*($totald - $idled)/$totald)/100}")
temp=$(sensors | grep temp1 | cut -d ":" -f 2 | cut -d "+" -f 2 | cut -d "." -f 1)
echo "cpu: ${cpu}% ${temp}°C"