编写服务器重启的脚本(rc.local调用版本)

我们在做一些兼容性测试时,经常需要做一些服务器reboot 或者DC的测试,以检查里边一些设备(网卡,硬盘等)信息,之前讲过用定时器crontab来编写,下面介绍一下最为常用的方式:在启动项(rc.local)里调用
1 首先,明确以下思路, linux OS 每次重启的时候,都会调用一下启动项,那我们就可以将脚本放在里边执行
确定打开rc-local.service
systemctl enable rc-local.service
systemctl start rc-local.service
chmod -R 777 /etc/rc.d/*
2 我们可以在脚本路径里创建一个重启次数的文件,用来判断重启次数
这里注意以下,需要将你的脚本在rc.local里后台运行

#!/bin/bash
lujing=$(cd $(dirname $0); pwd)
cd  $lujing
model=$1
cycleNum=$2
#echo "judge reboot or dc"
function judgeType(){
	if [ "$model" == "reboot" ];then
       reboot
	elif [ "$model" == "dc" ];then
	    ipmitool raw 0 2 2
	else
		echo "please input correct type"
		exit -1
	fi
}
if [ -f  "./num.log" ]
then
    echo "$model is running"
else    
    systemctl enable rc-local.service
    systemctl start rc-local.service
    chmod -R 777 /etc/rc.d/*
    echo "$model cycle test start"
    echo 0 >num.log
    echo " nohup bash $lujing/$0  $model $cycleNum &" >>/etc/rc.d/rc.local  
   
fi

3 就是判断cycle的次数
运行的时候 会有一个倒计时30秒的提示

num=`cat num.log`
#echo "start cycle test"
if [ $num == 0 ]
then 
        echo "service is start $model after 30s"            
        echo 1 >num.log
        num=`cat num.log`        
        t=30
        while test $t -gt 0
        do
            if [ $t -ge 10 ];then 
               echo -e "${t}\b\b\c"
            elif [ $t -eq 9 ];then
               echo -e "  \b\c"
               echo -e "\b${t}\b\c"
            else
               echo -e "${t}\b\c"
            fi
            sleep 1
            t=$((t-1))
        done       
        judgeType
elif [ $num == $cycleNum ]
then  
     echo "====================service has $model last $num times, finished `date '+%F_%H:%M:%S'` ============" >>time.log  
     cat /etc/rc.d/rc.local
     sed -i '$d'  /etc/rc.d/rc.local  
     exit -1
else  
    echo "===========service has $model $num times, `date '+%F_%H:%M:%S'` ==========" >>time.log
    let num++
    echo $num >num.log
    sleep 80
    judgeType   
fi

4 脚本运行起来后,如果想随时停止,就要把当前的进程杀掉,还需要进入启动项里,把加进去的命令注释掉。我们可以把此项操作写在脚本里,运行此脚本就让其生成一个停止测试的脚本
用EOF来实现

#echo "create a script of stop cycle"
cat << EOF > stop_${model}.sh 
#/bin/bash
str="$0"
EOF
cat << "EOF" >> stop_${model}.sh 
process=`ps -ef |grep $str|grep -v grep |awk '{print $2}'`
kill -9 $process
sed -i '$d'  /etc/rc.d/rc.local 
EOF

生成的stop脚本如下:

#/bin/bash
str="/root/Desktop/jason/reboot.sh"
process=`ps -ef |grep $str|grep -v grep |awk '{print $2}'`
kill -9 $process
sed -i '$d'  /etc/rc.d/rc.local 

5 最后将获取OS 的判断加进去,因为后面会做一下盘的检查或者读写操作等等,所以需要过滤OS盘

#echo "check os disk"
bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/sd" | sed 's/[0-9]//g' |sort -u|awk -F "/" '{print $NF}'`
if  test -z "$bootdisk"
then
        bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/nvme" | sed 's/p[0-9]//g' |sort -u|awk -F "/" '{print $NF}'`
        echo "os disk os $bootdisk"
else
        echo "os disk is $bootdisk"
fi

6 运行脚本
比如 :
bash xxx.sh reboot 100 :100次reboot
bash xxx.sh dc 100 : 100次dc
完整的脚本如下:

!/bin/bash
lujing=$(cd $(dirname $0); pwd)
cd  $lujing
model=$1
cycleNum=$2
#echo "judge reboot or dc"
function judgeType(){
	if [ "$model" == "reboot" ];then
       reboot
	elif [ "$model" == "dc" ];then
	    ipmitool raw 0 2 2
	else
		echo "please input correct type"
		exit -1
	fi
}
#echo "create a script of stop cycle"
cat << EOF > stop_${model}.sh 
#/bin/bash
str="$0"
EOF
cat << "EOF" >> stop_${model}.sh 
process=`ps -ef |grep $str|grep -v grep |awk '{print $2}'`
kill -9 $process
sed -i '$d'  /etc/rc.d/rc.local 
EOF
#echo "check os disk"
bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/sd" | sed 's/[0-9]//g' |sort -u|awk -F "/" '{print $NF}'`
if  test -z "$bootdisk"
then
        bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/nvme" | sed 's/p[0-9]//g' |sort -u|awk -F "/" '{print $NF}'`
        echo "os disk os $bootdisk"
else
        echo "os disk is $bootdisk"
fi

if [ -f  "./num.log" ]
then
    echo "$model is running"
else    
    systemctl enable rc-local.service
    systemctl start rc-local.service
    chmod -R 777 /etc/rc.d/*
    echo "$model cycle test start"
    echo 0 >num.log
    echo "bash $lujing/$0  $model $cycleNum" >>/etc/rc.d/rc.local     
fi
num=`cat num.log`
#echo "start cycle test"
if [ $num == 0 ]
then   
        echo "service is start $model after 30s"            
        echo 1 >num.log
        num=`cat num.log`        
        t=30
        while test $t -gt 0
        do
            if [ $t -ge 10 ];then 
               echo -e "${t}\b\b\c"
            elif [ $t -eq 9 ];then
               echo -e "  \b\c"
               echo -e "\b${t}\b\c"
            else
               echo -e "${t}\b\c"
            fi
            sleep 1
            t=$((t-1))
        done       
        judgeType
elif [ $num == $cycleNum ]
then  
     echo "====================service has $model last $num times, finished `date '+%F_%H:%M:%S'` ============" >>time.log  
     cat /etc/rc.d/rc.local
     sed -i '$d'  /etc/rc.d/rc.local  
     exit -1
else  
    echo "===========service has $model $num times, `date '+%F_%H:%M:%S'` ==========" >>time.log
    let num++
    echo $num >num.log
    sleep 80
    judgeType   
fi

你可能感兴趣的:(服务器,linux,bash)