dwmbar/modules/cpuload

33 lines
646 B
Text
Raw Normal View History

2019-10-22 12:12:44 +00:00
#!/bin/bash
# Prints out the CPU load percentage
PREFIX=' '
get_load()
{
2019-10-23 12:46:26 +00:00
# Get the first line with aggregate of all CPUs
cpu_last=($(head -n1 /proc/stat))
cpu_last_sum="${cpu_last[@]:1}"
cpu_last_sum=$((${cpu_last_sum// /+}))
sleep 0.05
cpu_now=($(head -n1 /proc/stat))
cpu_sum="${cpu_now[@]:1}"
cpu_sum=$((${cpu_sum// /+}))
cpu_delta=$((cpu_sum - cpu_last_sum))
cpu_idle=$((cpu_now[4]- cpu_last[4]))
cpu_used=$((cpu_delta - cpu_idle))
cpu_usage=$((100 * cpu_used / cpu_delta))
# Keep this as last for our next read
cpu_last=("${cpu_now[@]}")
cpu_last_sum=$cpu_sum
echo "$PREFIX$cpu_usage%"
2019-10-22 12:12:44 +00:00
}
get_load