当前位置: 首页 > article >正文

编写服务器重启的脚本(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

http://www.kler.cn/news/17335.html

相关文章:

  • 基于GWO灰狼优化算法的城市路径优化问题GWO-TSP(MATLAB程序)
  • 操作系统——线程调度
  • SpringBoot整合Mybatis-Plus、Jwt实现登录token设置
  • Java回收垃圾的基本过程与常用算法
  • 面试总结,4年经验
  • python语法入门到面向过程编程(二)
  • 类与对象之构造函数
  • SPSS如何进行基本统计分析之案例实训?
  • 什么是工业互联网?5G到底能在工业互联网中承担哪些重任呢?
  • Java实现添加文字水印、图片水印功能
  • java基础知识——27.动态代理
  • Linux指令大全——从零入门到实用工具
  • javaweb权限管理简单实现_javaweb管理系统项目
  • day21_IO
  • 使用 NutUI 搭建「自定义业务风格」的组件库 | 京东云技术团队
  • 【BeautifulSoup下】——05全栈开发——如桃花来
  • LeetCode:21. 合并两个有序链表
  • 1992-2022年31省GDP、第一产业增加值、第二产业增加值 第三产业增加值
  • 【51单片机】DS1302时钟模块
  • Office转换需要用到的SDK(建议)
  • 黑马点评项目导入
  • 操作系统内存管理笔记
  • nginx中使用absolute_redirect解决代理重定向问题
  • 【MySQL】数据库基础操作一:建库与建表
  • P4725 【模板】多项式对数函数(多项式 ln)
  • MobileNet(V1、V2、V3)入门
  • 状态机模式
  • 男子订民宿被毁约5个家庭漂泊街头 房东:住满了,没办法
  • Maven 知识点总结
  • 日撸 Java 三百行day38