当前位置: 首页 > Linux

cpu内存监控

时间:2023-04-06 02:53:50 Linux

linuxshell实战中的案例需求现在每3秒检测一次电脑的cpu和内存使用情况。如果内存占用或者cpu占用超过90%,给出警告代码如下(仅供参考,case适用于作者自己的MBP)echo"开始监控电脑cpu和内存占用:"#Cycleexecutionwhiletruedo#Totalmemorytotal=`expr1024\*8`echo-e"\n\n你mac的总内存是$totalM"#Usememoryuseds=$(top|head-n10|grepPhysMem|awk'{print$2}')#截取去掉Mused=${useds%*M}#求百分比percent=$(printf"%d"$((used*100/total)))#判断并输出结果echo"$usedM已用,占$percent%"if[$percent-gt90]thenecho'卧槽,内存快炸了不行,电脑不灵了...'elseecho'你的内存还正常,你可以放心使用^.^'fi#cpuvariableuser_cpu=$(top|head-n10|grepCPU|awk'{print$3}')syst_cpu=$(top|head-n10|grepCPU|awk'{print$5}')user_cpu=${user_cpu%*%}syst_cpu=${syst_cpu%*%}cpu_percent=$(echo"$user_cpu+$syst_cpu"|bc)echo-e"\n\nCpu用户使用情况$user_cpu%"echo"Cpusystemusage$syst_cpu%"echo"Cpuusage$cpu_percent%"if[$(echo"$cpu_percent>=90"|bc)=1]thenecho'操,CPU要崩溃了...'elseecho'你的cpu还能用,你可以放心使用^.^'fisleep3done