Jslfl【软件开发技术笔记】

Linux监控tomcat状态,自动重启

1.
monitor.html放到项目根目录,可以通过http://127.0.0.1/monitor.html访问到

2.
monitor_tomcat.sh放到服务器后

1
2
3
vi monitor_tomcat.sh
:set ff=unix
:wq

赋予monitor_tomcat.sh可执行权限:

1
chmod 777 monitor_tomcat.sh

手动执行一下monitor_tomcat.sh看是否能启动指定位置的tomcat

1
./monitor_tomcat.sh

3.
/etc/profile 中添加

1
2
export JAVA_HOME=/home/app/java/jdk1.8.0_231
export JRE_HOME=/home/app/java/jdk1.8.0_231/jre

执行source /etc/profile

tomcat/catalina.sh中添加

1
2
export JAVA_HOME=/home/app/java/jdk1.8.0_231
export JRE_HOME=/home/app/java/jdk1.8.0_231/jre

4.
添加进crontab中

1
crontab -e

添加每5分钟执行一次脚本进行tomcat检测

1
*/5 * * * * /home/app/monitor_tomcat.sh

如果 执行monitor_tomcat.sh出以下错误
-bash: ./monitor_tomcat.sh: /bin/sh^M: bad interpreter: No such file or directory

1
2
3
vi monitor_tomcat.sh
:set ff=unix
:wq

monitor.html

1
<html lang="en"><head></head><body>it's ok!</body></html>

monitor_tomcat.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# func:Automatically monitor Tomcat and perform restart operation
# Get Tomcat process ID(Where... In [grep - W '...'] needs to be replaced with the name of the Tomcat folder actually deployed, as follows)
TomcatID=$(ps -ef |grep tomcat |grep -w '/home/app/tomcat/apache-tomcat-8.5.47'|grep -v 'grep'|awk '{print $2}')
# Get the number of repeated starts of the same Tomcat
TomcatCount=$( ps -ef |grep tomcat |grep -w '/home/app/tomcat/apache-tomcat-8.5.47/tomcat_autoupdate'|grep -v 'grep'|awk '{print $2}' |wc -l)

# Tomcat launcher (note the actual installation path of Tomcat here)
StartTomcat=/home/app/tomcat/apache-tomcat-8.5.47/bin/startup.sh
TomcatCache=/home/app/tomcat/apache-tomcat-8.5.47/work

# Define the page address to monitor
WebUrl=http://127.0.0.1/monitor.html

# Log output
GetPageInfo=/tmp/tomcat_oip_back_visit.info
TomcatMonitorLog=/tmp/tomcat_oip_back_monitor.log

Monitor()
{
    echo "[info]Start monitoring Tomcat...[$(date +'%F %H:%M:%S')]"
    #Here, it's determined whether the Tomcat process has been repeatedly started.If it has been repeatedly started,all the processes will be killed
    if [[ $TomcatCount -gt 1 ]];then
      ps -ef |grep tomcat |grep -w '/home/app/tomcat/apache-tomcat-8.5.47/tomcat_autoupdate'|grep -v 'grep'|awk '{print $2}' | xargs kill -9
      echo "Open $tomcatcount processes in the same tomcat, and kill them all"
      sleep 5
    else
        if [ $TomcatID ];then #Here, judge whether the Tomcat process exists
                echo "[info]The current Tomcat process ID is:$TomcatID,Continue to test page..."
                # Check whether the startup is successful (if it is successful,the page will return to the status "200"),if there is no response in 100 seconds, it will not wait
                TomcatServiceCode=$(curl -s -o $GetPageInfo -m 100 --connect-timeout 100 $WebUrl -w %{http_code})
                if [ $TomcatServiceCode -eq 200 ];then
                        echo "[info]The return code of the page is $tomcatservicecode,Tomcat is started successfully,and the test page is normal"
                else
                        echo "[error]Error in Tomcat page, please note... Status code is $tomcatservicecode, error log has been output to $getpageinfo"
                        echo "[error]Page access error, start to restart Tomcat"
                        kill -9 $TomcatID # Kill Tomcat process
                        sleep 5
                        # rm -rf $TomcatCache # Clean up Tomcat cache
                        $StartTomcat
                fi
        else
                echo "[error]Tomcat process does not exist! Tomcat starts to restart automatically"
                echo "[info]$StartTomcat, please wait"
                # rm -rf $TomcatCache
                $StartTomcat
        fi
     fi
    echo "--------------------------"
}
Monitor>>$TomcatMonitorLog

,

Comments are currently closed.