dotfiles/scripts/cpu-utilization

21 lines
518 B
Bash
Executable File

#!/bin/bash
# Get first snapshot
read cpu user nice system idle iowait irq softirq steal guest < /proc/stat
total1=$((user+nice+system+idle+iowait+irq+softirq+steal))
idle1=$idle
sleep 0.5
# Get second snapshot
read cpu user nice system idle iowait irq softirq steal guest < /proc/stat
total2=$((user+nice+system+idle+iowait+irq+softirq+steal))
idle2=$idle
# Calculate usage
total_diff=$((total2 - total1))
idle_diff=$((idle2 - idle1))
cpu_usage=$((100 * (total_diff - idle_diff) / total_diff))
echo "$cpu_usage"