We recommend that you store this script on the Linux server where tomcat is located, and create the/root/script directory.
However, emails cannot be notified due to serious network, hardware, or system faults. Some functions of the script can be removed. It can be placed on other servers only for monitoring or restarting Tomcat.
#/Bin/bash
######################################## #####
# Author zhao yanan
# Date 2010/08/23
# Update 2011/01/10
# Automatic detection and restart tomcat
######################################## #####
# The script can be written to an automatic task. The execution cycle can be determined based on the Start and Stop times of tomcat, as shown below. Check every 10 minutes between and every day.
# Echo "*/10 0-20 **** root/script/tomcat_check.sh>/root/script/tomcat_check.log 2> & 1">/etc/crontab
Echo ===================================================== =
Date
Cd/root/script
Rm-f index.html # Delete the previously downloaded Homepage
Wget-T 10-t 3-q http://XXX.XXX.XXX.XXX/index.html # download the home page, after failure up to three times, each interval of 10 seconds
If [! -E index.html]; then # determines whether the homepage is successfully downloaded. If yes, the following task is executed; otherwise, the tasks after else are executed.
# Java Environment variables # Set jdk Environment variables based on your actual situation
Export JAVA_HOME =/opt/jdk1.6.0 _ 21
Export CLASSPATH =.: $ JAVA_HOME/lib/tools. jar
Export CATALINA_HOME =/opt/apache-tomcat-6.0.26
Export PATH = $ JAVA_HOME/bin: $ PATH
Echo "restart tomcat ."
$ {CATALINA_HOME}/bin/shutdown. sh # disable tomcat
Sleep 150 # Wait for tomcat to close, usually 1 ~ S
Kill-9 'ps aux | grep java | grep-v grep | awk {print $2} '# force the tomcat process to end after the wait time ends.
Sleep 5
$ {CATALINA_HOME}/bin/startup. sh
Sleep 5 # Wait for tomcat to start, usually 1 ~ S
/Usr/sbin/sendmail-t <EOF # Send email notification. Do not add spaces before the following items. Delete this comment during production.
From: web server <root @ localhost> # mail sender. Delete this comment during production.
To: username@gmail.com # Master mail address, note, most mail receiving servers do not accept messages without fully-qualified, generally will fail, please delete this comment during production
Cc: username@gmail.com # Cc Mail address, delete this comment during production
Subject: Alarm: tomcat restart # mail title. Delete this comment during production.
---------------------------------- # Email content. Delete this comment during production.
In just restarted tomcat.
----------------------------------
EOF # there must be no space before this symbol. Delete this comment during production.
Else
Echo "checked." # The home page is successfully downloaded, prompting you to check
Fi
The script file in the attachment contains the DOS Line Break (CR/LF). It is no problem to copy the content of the file to the SSH client. If the file is directly copied to the Linux system, run the following command first:
Mv example_tomcat_check.txt example_tomcat_check.sh
Dos2unix example_tomcat_check.sh