你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

嵌入式学习笔记-记录系统启动次数

2021/12/24 6:15:40

在实际应用的过程中,我偶尔会用到计算开发板的启动次数,最笨的方法将所有log保存,最后就是查看log,数一下启动次数,可以通过查找关键字的方式(当然,我不会笨到启动一次记录一次的,嘿嘿)。感觉这样太麻烦了,现在都啥时代了,还这样做。

准备工作,首先要找到你系统启动后自动运行的脚本,我的板子是/autorun.sh ,这样做的原因是,在你没看到或者不小心启动了或者复位了也能监测到,你如果要求手动运行监测脚本,那么这种歌情况就不能记录了。让后将下面的脚本拷贝到这个自启动脚本。后来就写了这个脚本

脚本1:

root@fetmx6ull-s:/root# cat /autorun.sh 
#!/bin/bash 

# first mode ,calc the count of file ,
runcount=1
for i in `find /root/ -name "*.log"`
do	
	runcount=`expr ${runcount} + 1`
done
echo "system is run ${runcount} counts"
echo "power on" >> /root/${runcount}.log 
sleep 1s

执行如下:

root@fetmx6ull-s:/root# rm *        
root@fetmx6ull-s:/root# /autorun.sh 
system is run 1 counts
root@fetmx6ull-s:/root# /autorun.sh 
system is run 2 counts
root@fetmx6ull-s:/root# /autorun.sh 
system is run 3 counts
root@fetmx6ull-s:/root# /autorun.sh 
system is run 4 counts
root@fetmx6ull-s:/root# /autorun.sh 
system is run 5 counts
root@fetmx6ull-s:/root# ls
1.log  2.log  3.log  4.log  5.log
root@fetmx6ull-s:/root# 

脚本2:

上面的脚本是通过每次启动建立一个文档,通过计算文档个数来完成,这个有几个不好的地方,如当其他脚本也不小心生成了  。log 文件,就会导致计算错误,或者当启动次数多了,文件都比较多,查找看着乱,那么就产生了第二个脚本;

这个脚本是通过查看 /runcount.log中的行数来判断当前启动了多少次,每启动一次i增加一行,并且把启动时间进行记录,注意,时间必须在开发板有电池的情况才是可以的,否则时间不能作为参考项。 

执行如下:

root@fetmx6ull-s:/root# /autorun.sh 
cat: can't open '/root/runcount.log': No such file or directory
18:49:02:  power is run 1 counts
root@fetmx6ull-s:/root# /autorun.sh 
18:49:10:  power is run 2 counts
root@fetmx6ull-s:/root# /autorun.sh 
18:49:12:  power is run 3 counts
root@fetmx6ull-s:/root# /autorun.sh 
18:49:14:  power is run 4 counts
root@fetmx6ull-s:/root# /autorun.sh 
18:49:16:  power is run 5 counts
root@fetmx6ull-s:/root# ls
runcount.log
root@fetmx6ull-s:/root# cat runcount.log 
18:49:02: power is run 1 counts
18:49:10: power is run 2 counts
18:49:12: power is run 3 counts
18:49:14: power is run 4 counts
18:49:16: power is run 5 counts

怎么样,又可以偷懒了吧?嘿嘿。

要相信  ,懒人推动社会进步。。。。。。

喜欢吗,关注并留言吧。