shell 判断tomcat状态 >500 就重启

shell 判断tomcat状态 >500 就重启

摘要

shell 判断tomcat状态 >500 就重启

判断tomcat状态>500就重启

#/bin/bash

check_url="http://api.server.com/health"
log_tag="_tomcat8080"
restart_sh="/data/tomcat_api_8080/bin/restart.sh"

if [ ! -f /tmp/down_count$log_tag ];then
    echo "0" > /tmp/down_count$log_tag
fi

code=`curl --connect-timeout 10 -m 60 --head --silent $check_url | awk 'NR==1{print $2}'`
if [ -n "$(echo $code| sed -n "/^[0-9]\+$/p")" ];then
    echo "code is number"
else
    code=555
fi
echo $code

if [ "${code}" -ge 500 ];then
    down=`expr $(cat /tmp/down_count$log_tag) + 1`
    echo "$down" > /tmp/down_count$log_tag
    echo "计数失败一次"
    cat /tmp/down_count$log_tag
    if [ "$down" -ge 3 ];then
        if [ ! -f "/tmp/restart_count$log_tag" ];then
            echo "0" > /tmp/restart_count$log_tag
        fi
        echo "失败3次,开始重启"
        restart_count=`expr $(cat /tmp/restart_count$log_tag) + 1`
        echo "$restart_count" > /tmp/restart_count$log_tag
        nohup sh +x $restart_sh >/dev/null 2>&1 &
        #echo "重启超过2次,不再重启"
        #if [ "$restart_count" -le 2 ];then
        #        sh +x  $restart_sh
        #        echo "tomcat down at `date`" >> /tmp/down_info$log_tag
        #fi

    fi
else
    echo "0" > /tmp/down_count$log_tag
    echo "0" > /tmp/restart_count$log_tag
fi

脚本解释

服务没有启动的时候,curl请求会被直接关闭,返回空,所以要区分是否是数字类型的返回值.

失败计数达到3次才重启server.
注释掉的部分意思是,重启了2次还不行就不再重启了,只是记录日志.

每分钟执行一次

crontab -e
* * * * *  /bin/bash /root/check_debugData.sh > /root/check_debugData.log