- #!/bin/bash
- # by Paul Colby (http://colby.id.au), no rights reserved ;)
- PREV_TOTAL=0
- PREV_IDLE=0
- while true; do
- CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
- #echo "${CPU[0]}";
- # echo "${CPU[1]}";
- # echo "${CPU[2]}";
- # echo "${CPU[3]}";
- # echo "${CPU[4]}";
- unset CPU[0] # Discard the "cpu" prefix.
- IDLE=${CPU[4]} # Get the idle CPU time.
- # Calculate the total CPU time.
- TOTAL=0
- for VALUE in "${CPU[@]}"; do
- let "TOTAL=$TOTAL+$VALUE"
- done
- # echo $TOTAL;
- # Calculate the CPU usage since we last checked.
- let "DIFF_IDLE=$IDLE-$PREV_IDLE"
- let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
- #获取不带小数的百分比
- #let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
- #echo -en "\rCPU: $DIFF_USAGE% \b\b"
- #获取带小数的百分比
- let "DIFF_USAGE=1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL"
- let "DIFF_USAGE_UNITS=$DIFF_USAGE/10"
- let "DIFF_USAGE_DECIMAL=$DIFF_USAGE"
- echo -en "\rCPU: $DIFF_USAGE_UNITS.$DIFF_USAGE_DECIMAL% \b\b\b\b"
- # Remember the total and idle CPU times for the next check.
- PREV_TOTAL="$TOTAL"
- PREV_IDLE="$IDLE"
- # Wait before checking again.
- sleep 1
- done
使用方法:/bin/bash cpu_usage.sh
CPU: 7.3%
注意:以上脚本只能手动结束运行
阅读(3167) | 评论(0) | 转发(0) |