Files
dotfiles/scripts/cpu

29 lines
709 B
Bash
Executable File

#!/bin/sh
# Script to display CPU usage
# Cache in tmpfs to improve speed and reduce SSD load
cache=/tmp/cpu-script-cache
rm /tmp/cpu-script-cache 2>/dev/null
while true; do
sleep 1
cpu_stats=$(awk '/^cpu / {print $2 + $3 + $4 + $5, $5}' /proc/stat)
if [ ! -f "$cache" ]; then
printf "  ---%% \r"
echo "$cpu_stats" >"$cache"
continue
fi
prev_stats=$(cat "$cache")
echo "$cpu_stats $prev_stats" | awk '{
total_diff = $1 - $3
idle_diff = $2 - $4
if (total_diff == 0) {
usage = 0
} else {
usage = 100 * (1 - idle_diff / total_diff)
}
printf "  %.1f%% \r", usage
exit
}'
echo "$cpu_stats" >"$cache"
done