21 lines
498 B
Bash
Executable File
21 lines
498 B
Bash
Executable File
#!/usr/bin/env bash
|
|
# Read /proc/stat CPU utilization
|
|
read -r cpu u n s i io ir st g gn < /proc/stat
|
|
total1=$((u + n + s + i + io + ir + st + g + gn))
|
|
idle1=$((i + io))
|
|
sleep 0.2
|
|
read -r cpu u n s i io ir st g gn < /proc/stat
|
|
total2=$((u + n + s + i + io + ir + st + g + gn))
|
|
idle2=$((i + io))
|
|
|
|
total_diff=$((total2 - total1))
|
|
idle_diff=$((idle2 - idle1))
|
|
|
|
if [ "$total_diff" -gt 0 ]; then
|
|
cpu_usage=$(( (total_diff - idle_diff) * 100 / total_diff ))
|
|
else
|
|
cpu_usage=0
|
|
fi
|
|
|
|
echo "$cpu_usage"
|