linux CPU利用率统计


Bash脚本统计CPU实时利用率


#!/bin/bash
CPU_LOG_1=`cat /proc/stat | grep 'cpu ' | awk '{print $2,$3,$4,$5,$6,$7,$8}'`
SYS_IDLE_1=`echo ${CPU_LOG_1} | awk '{print $4}'`
TOTAL_1=`echo ${CPU_LOG_1} | awk '{print $2+$3+$4+$5+$6+$7+$8}'`
sleep 5
CPU_LOG_2=`cat /proc/stat | grep 'cpu ' | awk '{print $2,$3,$4,$5,$6,$7,$8}'`
SYS_IDLE_2=`echo ${CPU_LOG_2} | awk '{print $4}'`
TOTAL_2=`echo ${CPU_LOG_2} | awk '{print $2+$3+$4+$5+$6+$7+$8}'`
SYS_IDLE=`expr ${SYS_IDLE_2} - ${SYS_IDLE_1}`
TOTAL=`expr ${TOTAL_2} - ${TOTAL_1}`
SYS_IDLE_RATE=`expr $SYS_IDLE/$TOTAL*100 | bc -l`
SYS_RATE=`expr 100-$SYS_IDLE_RATE | bc -l`
Dis_SYS_RATE=`expr "scale=3; ${SYS_RATE}/1" | bc`
echo $Dis_SYS_RATE


你可能感兴趣的:(linux,cpu,use,Rate)