脚本:

for((i=1;i<=5;));  
do   
    cpu_us=`top -bn 1 | grep 'Cpu(s)' | awk -F'[" "%]+' '{print $3}'`
    cpu_sy=`top -bn 1 | grep 'Cpu(s)' | awk -F'[" "%]+' '{print $5}'`
    #echo $cpu_us
    #echo $cpu_sy
    cpu_sum=$(echo "$cpu_us+$cpu_sy"|bc)
    nowdate=$(date +"%Y-%m-%d %H:%M:%S")
    echo "$nowdate CPU_SUM: $cpu_sum%" >> ./cpu.log
    sleep 120
done

挂载到后台启动:

nohup sh cpu.sh >/dev/null  2>&1 &

查看cpu.log文件,会发现每隔两分钟会写入一次CPU占用百分比 

Logo

更多推荐