Linux regularly checks tomcat status and automatically restarts when it goes down.
Put the script directly:
#! /Bin/bash
# Author: jgd
# Date: 2017-09-15
# Automatically detects a tomcat status and restarts when an exception occurs.
# ---- Start ----
# Obtain the tomcat process ID and modify it according to your actual situation, such as the port and tomcat name
TomcatID = $ (ps-ef | grep tomcat | grep 9085 | grep-w 'apache-tomcat-9.0.0.M21 '| grep-v 'grep' | awk' {print $2 }')
# Obtain the number of tomcat processes and modify them according to your actual situation, such as the port and tomcat name
TomcatCount = 'ps-ef | grep tomcat | grep 9085 | grep-W' apache-tomcat-9.0.0.M21 '| grep-v 'grep' | wc-l'
# Tomcat Startup Program (here, pay attention to the actual installation path of tomcat)
StartTomcat =/home/zyj13424063620/tomcat9085/apache-tomcat-9.0.0.M21/bin/startup. sh
TomcatCache =/home/zyj13424063620/tomcat9085/apache-tomcat-9.0.0.M21/work
# Define the page address to be monitored. You can modify the address based on your actual project.
WebUrl = http: // localhost: 9085/CommonCloud/views/index. jsp
# Log output, which can be modified based on your server
GetPageInfo =/home/zyj13424063620/myshell/logs/9085/tomcatMonitor.info
TomcatMonitorLog =/home/zyj13424063620/myshell/logs/9085/tomcatMonitor. log
Monitor ()
{
Echo "[info] Start monitoring tomcat... [$ (date + '% F % H: % M: % s')]"
If [$ TomcatCount-gt 0]; then # Check whether the TOMCAT process exists.
Echo "[info] current tomcat process ID: $ TomcatID. Continue to check the page ..."
# Check whether the startup is successful (if successful, the page will return the status "200 ")
TomcatServiceCode = $ (curl-s-o $ GetPageInfo-m 10 -- connect-timeout 10 $ WebUrl-w % {http_code })
If [$ TomcatServiceCode-eq 200]; then
Echo "[info] The return code of the page is $ TomcatServiceCode. tomcat is started successfully. The page is normal ......"
Else
Echo "[error] tomcat page error, status code: $ TomcatServiceCode, error log output to $ GetPageInfo"
Echo "[info] Page access error, start to restart tomcat"
Kill-9 $ TomcatID # kill the original tomcat Process
Sleep 3
Rm-rf $ TomcatCache # Clear tomcat Cache
Echo "[info] restart tomcat now ..."
Echo "[info] Please wait ..."
$ StartTomcat
Fi
Else
Echo "[error] the tomcat process does not exist! "
Echo "[info] Get information atatid: $ tomctomcatid"
Echo "[info] obtaining information atatcount: $ tomctomcatcount"
Echo "[info] Automatic Restart of tomcat"
Echo "[info] $ StartTomcat, please wait ......"
Rm-rf $ TomcatCache
$ StartTomcat
Fi
Echo "------------------------------"
}
Monitor> $ TomcatMonitorLog
The above is the shell script that detects and automatically restarts tomcat. You need to add
Edit the timer command:
Crontab-e
For example:
The script is executed once every five minutes.
.
Note: To automatically restart tomcat, You need to modify Catalina. sh in the bin directory of tomcat to specify the jdk or jre installation path, such as adding