Live2D

shell脚本检查tomcat应用健康状态

1.首先应用编写一个接口(http均可)

接口查询数据库中随便一个表一条数据,正常返回数据这表示应用健康,否则应用运行异常。

@RequestMapping(value = "/heartbeat", method = RequestMethod.GET)
public String heartbeat(){    
        //TODO  查询数据库数据(检测应用的同时检测数据库情况)
        List<User> users = queryByUser();
        log.info("=================hart==============心跳请求!");
        if(users.size()>0){
            return "OK";
        }
        return "OK";
    }

2.入口shell脚本(check_web_health.sh)

curl发起请求检查上面接口能否正常返回,总共三次请求,若有一次返回接口正常,则停止脚本执行;若三次都请求失败,则调用重启tomcat脚本(restartup_tomcat.sh)

#!/bin/bash
#Description: check web server
source /etc/profile
LOG="/home/jiankong_shell/log"
DATE=`date +%y%m%d-%H:%M`
a=0
mkdir -p /home/jiankong_shell/log
 
while [ $a -lt 3 ]
do
    RESULT=$(curl --connect-timeout 15 -m 20 http://127.0.0.1:8080/system/heartbeat)
   
    echo -----------------$RESULT
   
    if [ $RESULT ] && [[ $RESULT -eq "OK" ]]
    then
        echo ****************success****************$RESULT
        break
    fi
    a=`expr $a + 1`
#    sleep 10
done
 
if [ $a -eq 3 ]
then   
   # start_tomcat restart
    /home/jiankong_shell/restartup_tomcat.sh
    echo **[$DATE]****************error[$a]*****restart***********>>$LOG/$DATE.down
fi

3.重启tomcat脚本(restartup_tomcat.sh)

这里直接通过杀进程方式停止tomcat,然后在重启tomcat

#!/bin/bash
#执行命令,重启tomcat
source /etc/profile
tomcat_home=/usr/local/apache-tomcat-xxxx
JAVA_HOME=/usr/local/java/jdk1.8.0_45
export JAVA_HOME
#停止tomcat变
SHUTDOWN=$tomcat_home/bin/shutdown.sh
#启动tomcat变量
STARTTOMCAT=$tomcat_home/bin/startup.sh
#ps -ef |grep test | grep -v grep |awk '{print $2}'|xargs kill -9 -p
PID=`ps -ef |grep usr/local/apache-tomcat-workflow-new/conf  |grep -v grep | awk '{print $2}'`
echo $PID
if [ ! "$PID" ];then  # 这里判断TOMCAT进程是否存在
   echo "进程不存在"
else
   echo "进程存在 杀死进程PID$PID"
   kill -9 $PID
fi
$STARTTOMCAT
echo "success"

 

posted @ 2022-10-16 19:23  键盘命  阅读(230)  评论(0编辑  收藏  举报