#!/bin/bash # 检查服务是否可用,判断服务状态 function checkHealth() { currTime=`date +"%Y-%m-%d %H:%M:%S"` echo "$currTime $@"; # 以列表的方式一次性打印所有的参数 ConsulResult=$(curl -s --connect-timeout 500 --max-time 2 http://$1/actuator/health ) target="UP" local health=1 if [[ $ConsulResult =~ $target ]] then echo "$2结果健康"; else sleep 1s; ConsulResult=$(curl -s --connect-timeout 500 --max-time 2 http://$1/actuator/health ) if [[ $ConsulResult =~ $target ]] then echo "$2结果健康"; else sleep 1s; ConsulResult=$(curl -s --connect-timeout 500 --max-time 2 http://$1/actuator/health ) if [[ $ConsulResult =~ $target ]] then echo "$2结果健康"; else health=0; echo "$2 结果不健康"; fi fi fi return $health; } # 检查服务健康状态,并处理异常 function dealCheckResult() { ipport=$1 name=$2 echo "start $name =======================" checkHealth $ipport $name health=$? if [[ $health -eq 1 ]] then echo "$name 检查完毕,无问题!" else echo "重启 $name 预计2分钟" # 重启服务 docker stop $name sleep 5s docker start $name sleep 120s # 重新检查一次是否启动成功,不成功则再休眠60s checkHealth $ipport $name health=$? currTime=`date +"%Y-%m-%d %H:%M:%S"` if [[ $health -eq 1 ]];then echo "$currTime $name 服务已重启完成" else echo "$currTime $name 服务尚未重启完成,再休眠60s等待" sleep 60s fi fi echo "end $name 所有处理完毕===========" } # 获取系统启动时间 uptime=$(cat /proc/uptime) echo "uptime: $uptime" uptimeArr=(${uptime//./ }) startTime=${uptimeArr[0]} echo $startTime a=`expr $startTime + 1` b=1800 # 系统启动30分钟内不执行 if [[ "$a" -lt "$b" ]]; then echo "系统启动30分钟内不执行" exit fi echo "启动时间为$startTime秒,正常执行脚本" echo "先关闭定时任务" /sbin/service crond stop ipport1=172.16.1.122:8181 name1=datacenter dealCheckResult $ipport1 $name1 ipport2=172.16.1.122:8282 name2=unit dealCheckResult $ipport2 $name2 ipport3=172.16.1.122:8383 name3=core dealCheckResult $ipport3 $name3 echo "重新启动定时任务" /sbin/service crond start