这几天写作2篇关于top的文章了,总是发现刚解决一个问题,第二个问题就出来了,总是不那么完美,好了,上主菜:
我在实验的时候用的脚本如下:
#!/bin/bash
while true
do
echo "*********************************************************************"
temperature=`cat /sys/class/thermal/thermal_zone0/temp`
#temperature=`echo "scale=3;$temperature/1000" | bc`
echo "`date +%T` current temperature= $temperature runtime = `cat /proc/uptime`" | tee -a ./result.log
echo "`date +%T` current freq= `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`" | tee -a ./result.log
top -n 1 | grep '%Cpu' -A 0 | tee -a ./top.log
sleep 60s
echo ""
done
其中top的语句的意思是执行一次top,将打印出来的包含%Cpu的字符打印出来,同时 保存到top。log中,实际使用的时候发现结果不对,一旦后台执行就莫名其妙的终止了,删除这一句就正常了,说明其他的没有问题。
从网上查找资料 ,top后台无法执行解决办法_tzh_linux的专栏-优快云博客 从这里查到可以使用-b 参数,才能后台执行 ,于是就更改成了 top -b -n 1 | grep '%Cpu' -A 0 | tee -a ./top.log & ,然后一执行,得,发现一个别的问题,现象是,当我后台这行这个脚本的时候(我的这个脚本的名称是run.sh),发现输出倒是正常,但是使用ps,不能发现run.sh的进程干脆没办法结束了,使用 ps -ef 也不行,只能reboot,说明还是没有完全解决问题,后来想既然我的外面的脚本使用后台运行,这里就把 & 去掉,修改完成后在执行,结果正确,也能结束进程,完美解决;
全部脚本如下:
#!/bin/bash
rm *.log
while true
do
echo "**********************************************************"
temperature=`cat /sys/class/thermal/thermal_zone0/temp`
temperature=`echo "scale=3;$temperature/1000" | bc`
echo "`date +%T` current temperature= $temperature runtime = `cat /proc/uptime`" | tee -a ./result.log
echo "`date +%T` current freq= `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`" | tee -a ./result.log
#vmstat 1 3 | tee -a ./result.txt
echo "`date +%T` `top -b -n 1 | grep '%Cpu' -A 0`" | tee -a ./top.log
sleep 5s
echo ""
done
感觉有用处的,就点个赞吧!
精准定位需求,解决实际问题;理论结合实际,共同努力提高。
本文分享了一次解决top命令后台运行终止问题的经历,并提供了一个完整的shell脚本示例,该脚本能够持续监控系统的温度、CPU频率及CPU使用率等信息。
2233

被折叠的 条评论
为什么被折叠?



