Shell实现记录重启次数

最近在测试项目的时候要实现一个每启动一次记录一下次数的头疼问题。因为每次重启,如果用变量来写,就会失效。这时候好多人就不晓得用什么方法了,哈哈哈哈!这里呢我就提供一个思路:

  首先呢,在我们每次重启之前,将一个时间戳记录到一个文件中(boot.log),特别注意:写上sync,同步一下;【时间戳显示格式:$(date '+ %Y-%m-%d %H:%M:%S')】

然后每次重启的时候读取boot.log的行数,然后打印出来作为记录重启的次数。

 

echo "============reboot at $(date '+ %Y-%m-%d %H:%M:%S') ===================" >> boot.log
sync
count=$(wc -l boot.log | cut -d ' ' -f1)
echo -e "当前第 $count 次启动"echo -e "Next, it will restart in 3s..."sleep 3
reboot

 

posted @ 2023-02-17 21:30  弹弹大魔王  阅读(27)  评论(0编辑  收藏  举报