dotfiles/scripts/status/cpustat
2024-12-28 14:54:46 -06:00

21 lines
588 B
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# CPU usage %
# 'https://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux'
# Yeti Oct 21, 2017 at 9:21
if [ -n "$XDG_CACHE_HOME" ] ; then
cache_file="$XDG_CACHE_HOME"/cpustat
elif [ -n "$HOME" ] ; then
mkdir -p "$HOME/.local/cache"
cache_file="$HOME/.local/cache"/cpustat
else
exit 1
fi
if [ -f "$cache_file" ] ; then
cat "$cache_file"
fi
load=$({ head -n1 /proc/stat;sleep 0.3;head -n1 /proc/stat; } | awk '/^cpu /{u=$2-u;s=$4-s;i=$5-i;w=$6-w}END{print int(0.5+100*(u+s+w)/(u+s+i+w))}')
echo "$load%" > "$cache_file"