Have you ever had the problem that your online service suddenly goes down and then you get all kinds of calls or alerts… Let people not worry at all, today I will interpret for you the automatic restart script of the service hanging, by the listener to detect whether the deployed service is running normally, if not running, then restart.

Connect to the server vim/TMP /monitor.sh on which the service is deployed. You can simply copy the following command to change the underline

#! /bin/sh
# func: Automatically monitors Tomcat and restarts it if an exception occurs
Apache-tomcat-7.0.90 specifies the tomcat process ID(version of Tomcat)
TomcatID = $(ps – ef | grep tomcat | grep -w ‘apache tomcat – 7.0.90’ | grep -v ‘grep’ | awk ‘{print $2})
echo $TomcatID
# Tomcat startup command, which corresponds to the start.sh file of tomcat startup
StartTomcat = / root/apache tomcat – 7.0.90 / bin/startup. Sh
GetPageInfo=/tmp/tomcatMonitor.Info
The project URL after tomcat is started is equivalent to asking the server to initiate an HTTP request. If it succeeds, the service is started normally

# XXX is the actual project address

WebUrl = http://127.0.0.1:8080/XXX/index.html
Monitor()
{
Echo “[info] Start monitoring Tomcat… [$(date +’%F %H:%M:%S’)]”
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 –connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ]; then
Echo “[info] the return code is $TomcatServiceCode. [$(date +’%F %H:%M:%S’)]”
else
kill -9 $TomcatID
sleep 3
Echo “[info] Start Tomcat… [$(date +’%F %H:%M:%S’)]”
$StartTomcat
fi
}
Monitor >> /tmp/monitor.log


Now that the script is ready, let the script work:

Run the vim /etc/rc.local command as user root to edit the file


Add monitor.sh to the end of the file:



Run the root command to grant the rc.local execution permission.


chmod 777 /etc/rc.local


Power off and then power on to test whether the service starts automatically.