Cpu-usage.sh

From DarkWiki
Jump to: navigation, search

This script demonstrates how to calculate the current CPU usage (as a percentile) on a Ubuntu box. It takes into account the number of processors available.

Script

#!/bin/bash

CPUCOUNT=$(cat /proc/cpuinfo | grep processor | wc -l)
LOADAVG=$(cat /proc/loadavg | awk '{print $1}')

NRML=$(echo "$LOADAVG * 100 / $CPUCOUNT" | bc -l | sed "s/\(.*\...\).*$/\1/g")

echo $NRML