#!/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"