服务器测试中,经常会需要我们写reboot的脚本,然后检查每次重启的时候,里边信息输出是否有error。
通常下,都是在启动项/etc/rc.d/rc.local 执行reboot ,然后自己写一个count计数判断,不过这样的话,代码在rc.local执行的话,容易造成进不了桌面的情况。
下面介绍另一种reboot写法:crontab定时执行reboot脚本
1 crontab 执行脚本的写法:
echo "*/1 * * * * cd $PWD;sleep $time;sudo bash reboot.sh $1;" > $PWD/result/crontab.conf
这行代码的意思是每一分钟 执行后边的进程一次
2 先写一个start.sh .用来调用reboot.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo "input error, please input reboot times! $#"
exit 0
fi
PWD=`pwd`
timenow=`date +%Y.%m.%d--%H:%M:%S`
if [ -e $PWD/result ]
then
rm -rf $PWD/result
fi
mkdir -p $PWD/result
echo "SUT start reboot $1 cycles at $timenow" >$PWD/result/reboot_count.log
echo "*/1 * * * * cd $PWD;sleep 5;sudo bash reboot.sh $1;" > $PWD/result/crontab.conf
crontab $PWD/result/crontab.conf
3 写reboot脚本
#!/bin/bash
if [ $# -lt 1 ];then
echo "input error, please input reboot times! $#"
exit 0
fi
PWD=`pwd`
count=`cat $PWD/result/reboot_count.log | wc -l`
if [ $count -le $1 ]
then
echo "${count}:`date`" >> $PWD/result/reboot_count.log
sudo reboot
else
echo "reboot finish" >> $PWD/result/reboot_count.log
crontab -r
fi
4 验证:跑10 次reboot ;最好在/home 下运行
sh start.sh 10