利用crontab写服务器reboot的脚本

服务器测试中,经常会需要我们写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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值