Files
dotfiles/config-files/.local/bin/sb-cpu

27 lines
767 B
Bash
Executable File

#!/bin/sh
# Module showing overall CPU usage as a percentage.
# Cache in tmpfs to improve speed and reduce SSD load
cache=/tmp/cpubarscache
# Extract CPU stats (total time and idle time)
cpu_stats=$(awk '/^cpu / {print $2 + $3 + $4 + $5, $5}' /proc/stat)
# Check if cache exists, if not, initialize it
[ ! -f $cache ] && echo "$cpu_stats" > "$cache"
# Read previous CPU stats from cache
prev_stats=$(cat "$cache")
# Calculate CPU usage percentage and exit immediately
echo "$cpu_stats $prev_stats" | awk '{
total_diff = $1 - $3
idle_diff = $2 - $4
usage = 100 * (1 - idle_diff / total_diff)
printf " %.1f%%\n", usage
exit # Terminate script after printing the first line
}'
# Update cache with current stats
echo "$cpu_stats" > "$cache"