automatically restarts tomcat after monitoring tomcat down on Linux
- First step edit:monitor.sh file (file contents see below)
- Step Two install crontab (see below for installation steps)
- The third step is to add a timed task: CRONTAB-E
*/1 * * * */usr/java/monitor.sh
- monitor.sh file contents (configured under Manual execution:./monitor.sh See if you can start a hung tomcat)
=========monitor.sh start ===============
#!/bin/sh
#定义环境变量 (to change to your JDK-related address)
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.7.0_79/bin
Export java_home=/usr/java/jdk1.7.0_79
Export classpath= $JAVA _home/lib: $JAVA _home/lib/tools.jar
Export path= $PATH: $JAVA _home/bin
# Get the Tomcat process ID (note here that TOMCAT7 is changing to its own Tomcat directory name)
tomcatid=$(ps-ef |grep tomcat |grep-w ' tomcat7 ' |grep-v ' grep ' |awk ' {print $} ')
# Tomcat Launcher (note here to change the path to your Tomcat's actual installation)
starttomcat=/usr/java/tomcat7/bin/startup.sh
Tomcatcache=/usr/java/tomcat/work
# You define the page address to monitor, the simpler the better, for example: Write a success on the page
WEBURL=HTTP://IP: Port/test.jsp
# Log Output (self-defined address for output monitoring logs and monitoring of error logs)
Tomcatmonitorlog=/usr/java/monitor/tomcatmonitor.log
Getpageinfo=/usr/java/monitor/pageinfo.log
Monitor()
{
echo "[info] starts monitoring tomcat ... [$(date + '%F%h:%m:%s ')] "
If [[$TomcatID ]];then # here to determine if the Tomcat process exists
echo "[info] current tomcat process ID : $TomcatID, continue to detect page ..."
# Check whether the start is successful (the page will return a status of "+ " if successful)
tomcatservicecode=$(Curl-s-o $GetPageInfo-M--connect-timeout $WebUrl-W%{http_code})
If [$TomcatServiceCode-eq ];then
echo "[info] page return code is $tomcatservicecode, Tomcat started successfully, test page normal ..."
Else
echo[Error]tomcat page error, please note ... The status code is $tomcatservicecodeand the error log is output to $getpageinfo "
echo "[Error] page access error, start reboot tomcat"
Kill-9 $TomcatID # Kill the original tomcat process
#sleep 3
#rm-rf $TomcatCache # Clean up the Tomcat cache
# $StartTomcat
Fi
Else
echo "[Error]tomcat process does not exist!tomcat start auto restart ..."
echo "[info] $StartTomcat, please wait ..."
#rm-RF $TomcatCache
$StartTomcat
Fi
echo "------------------------------"
}
Monitor>> $TomcatMonitorLog
========monitor.sh End =================
Installation: Yum install-y Vixie-cron
If the installation fails:A resource in Ngnix 404 can do the following:
Cd/etc/yum.repos.d
VI Ngnix.repo
Put http://nginx.org/packages/OS/OSRELEASE/$basearch/
Change to http://nginx.org/packages/centos/6/$basearch/
This is the address I found under the http://nginx.org/packages/ , of course, it's best if you can see if it matches your Linux system .
- To see If the crontab installation was successful
View crontab Task List: Crontab-l
viewing crontab Status: Service Crond Status
Start crontab: Service cron start
- See If crontab starts automatically
Chkconfig | grep Crond
Crond 0: off 1: off 2: enable 3: enable 4: Enable 5 : enable 6: off
View 2,3,4,5 whether to start
Other common commands:
See if Setup starts automatically: Chkconfig-list crond
Add power-on to the CentOS system automatically: Chkconfig--level crond on
Automatically restarts Tomcat after monitoring Tomcat down on Linux