Linux使用shell脚本定时把cpu使用率写入文件
Tags: CPUCPU使用率LinuxOS监控脚本脚本分享
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | -- 从进程角度相加cpu使用率 cat > /tmp/cpu_usage.sh <<"EOF" #!/bin/bash while [ true ]; do /bin/sleep 3 ps auxw|awk '{if (NR>1){print $3}}' > /tmp/cpu_list awk '{CPU_PER+=$1}END{print CPU_PER}' /tmp/cpu_list | awk '{ print $0"\t" strftime("%H:%M:%S",systime()) } ' >> /tmp/cpu.txt done EOF -- 获取top命令的cpu使用率 cat > /tmp/cpu_usage.sh <<"EOF" #!/bin/bash while true; do cpu=`top -n 1|grep Cpu|awk '{print $8}'|cut -c 1-4` echo "100-$cpu" | bc >> /tmp/cpu.txt sleep 3 done EOF nohup sh /tmp/cpu_usage.sh & |
其次就是使用nmon命令把OS统计信息写入文件:
1 | nmon -f -N -m /tmp/ -s 3 -c 6 |