#!/bin/sh #定义环境变量 (to be changed to its own 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 Tomcat Process ID (note here TOMCAT7 to change to its own Tomcat directory name) tomcatid=$ (ps-ef |grep tomcat |grep-w ' apache-tomcat-7.0.85 ' |grep-v ' grep ' |awk ' {print $} ') # Tomcat Launcher (note here to change the path to the actual installation of your Tomcat) starttomcat=/usr/local/tomcat/apache-tomcat-7.0.85/bin/startup.sh Tomcatcache=/usr/local/tomcat/apache-tomcat-7.0.85/work # own definition to monitor the page address, the simpler the page the better, such as: the page to write a success can weburl=http://127.0.0.1:8080/index.jsp # Log output (define your own address for output monitoring log and monitor error log) 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] The current Tomcat process ID is: $TomcatID, continue to detect the page ... " # Detect whether the startup succeeds (the successful session page will return the status "200") tomcatservicecode=$ (Curl-s-o $GetPageInfo-M--connect-timeout $WebUrl-W%{http_code}) If [$TomcatServiceCode-eq];then echo "[INFO] page return code for $TOMCATSERVICECODE,TOMCAT start successfully, test page is normal ..." Else echo [Error]tomcat page error, please note ... The status code is $tomcatservicecode and the error log has been exported to $getpageinfo " echo "[ERROR] page access error, start restart Tomcat" Kill-9 $TomcatID # Kill the original Tomcat process #sleep 3 #rm-RF $TomcatCache # clean Tomcat cache # $StartTomcat Fi Else echo [Error]tomcat process does not exist!tomcat start automatic restart ...] echo [INFO] $StartTomcat, please wait ... " #rm-RF $TomcatCache $StartTomcat Fi echo "------------------------------" } Monitor>> $TomcatMonitorLog |