#!/bin/sh ## echo user nice system idle iowait irq softirq CPULOG_1=$(cat /proc/stat | grep 'cpu ' | 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 1 CPULOG_2=$(cat /proc/stat | grep 'cpu ' | 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}') SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1` Total=`expr $Total_2 - $Total_1` SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l` SYS_Rate=`expr 100-$SYS_USAGE |bc -l` Disp_SYS_Rate=`expr "scale=3; $SYS_Rate/1" |bc` echo $Disp_SYS_Rate%
###perl实现
#!/usr/bin/perl use warnings; $SLEEPTIME=5; if (-e "/tmp/stat") { unlink "/tmp/stat"; } open (JIFF_TMP, ">>/tmp/stat") || die "Can't open /proc/stat file!\n"; open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n"; @jiff_0=; print JIFF_TMP $jiff_0[0] ; close (JIFF); sleep $SLEEPTIME; open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n"; @jiff_1= ; print JIFF_TMP $jiff_1[0]; close (JIFF); close (JIFF_TMP); @USER=`awk '{print \$2}' "/tmp/stat"`; @NICE=`awk '{print \$3}' "/tmp/stat"`; @SYSTEM=`awk '{print \$4}' "/tmp/stat"`; @IDLE=`awk '{print \$5}' "/tmp/stat"`; @IOWAIT=`awk '{print \$6}' "/tmp/stat"`; @IRQ=`awk '{print \$7}' "/tmp/stat"`; @SOFTIRQ=`awk '{print \$8}' "/tmp/stat"`; $JIFF_0=$USER[0]+$NICE[0]+$SYSTEM[0]+$IDLE[0]+$IOWAIT[0]+$IRQ[0]+$SOFTIRQ[0]; $JIFF_1=$USER[1]+$NICE[1]+$SYSTEM[1]+$IDLE[1]+$IOWAIT[1]+$IRQ[1]+$SOFTIRQ[1]; $SYS_IDLE=($IDLE[0]-$IDLE[1]) / ($JIFF_0-$JIFF_1) * 100; $SYS_USAGE=100 - $SYS_IDLE; printf ("The CPU usage is %1.2f%%\n",$SYS_USAGE);
2015职称计算机考试书PowerPoint2007中 .. 定价:¥45 优惠价:¥42 更多书籍 | |
2015年全国职称计算机考试教材(2007模 .. 定价:¥225 优惠价:¥213 更多书籍 |