Obtain the CPU usage script.
#!/bin/bash#gino#20150711CPULOG_1=`cat /proc/stat | head -n1 | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`SYS_IDLE_1=`echo $CPULOG_1 | awk '{print $4}'`Total_1=`echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`sleep 1CPULOG_2=`cat /proc/stat | head -n1 | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`SYS_IDLE_2=`echo $CPULOG_2 | awk '{print $4}'`Total_2=`echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`Total=`expr $Total_2 - $Total_1`SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`SYS_USAGE=`echo $SYS_IDLE/$Total*100 |bc -l`SYS_Rate=`echo 100-$SYS_USAGE |bc -l`Disp_SYS_Rate=`expr "scale=2; $SYS_Rate/1" |bc`printf "%4.2f%%\n" ${Disp_SYS_Rate}
Summary:
Formula for Calculating the total CPU usage
CPU time = user + system + nice + idle + iowait + irq + softirq + stealCPU time slice = CPU_2-CPU_1IDLE time = idle_2-idle_1CPU usage = 100 * (CPU time slice-IDLE Time) /CPU time slice
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.