Linux+crontab monitors tomcat and automatically restarts the Tomcat service after shutdown __linux

Source: Internet
Author: User
To perform the steps:

Reference: You can first understand crontab:

Installing Crontab services with Yum

Yum Install Vixie-cron

Installation successful, look at the common commands

/sbin/service crond Start//boot service

/sbin/service Crond Stop//Off service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//Reload Configuration

See Crontab Service Status: Service Crond status

Manually start the Crontab service: Service Crond start

To see if the Crontab service is set to boot, execute command: NTSYSV

To add a boot auto start to the CentOS system: Chkconfig--level Crond on


1. Create a table of contents:/usr/tomcat_sh/monitor

2. Put in the shell file:/usr/tomcat_sh/monitor.sh

@ Script Content:

#!/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


3. Mandate: chmod a+xmonitor.sh

4. Join Crontab Execution: crontab–e

*/1 * * * */usr/tomcat_sh/monitor.sh #一分钟检测一次

@ Description: Execution Time * * * *

· Minute: Represents a minute, which can be any integer from 0 to 59.

· Hour: Represents an hour, which can be any integer from 0 to 23.

· Day: Represents a date, which can be any integer from 1 to 31.

· Month: Represents the month, which can be any integer from 1 to 12.

· Week: Indicates the day of the week, can be any integer from 0 to 7, where 0 or 7 represent Sunday.

· Command: The commands you want to execute, either system commands or script files that you write yourself.

In each of these fields, you can also use the following special characters:

· Asterisk (*): represents all possible values, such as the month field if it is an asterisk, indicates that the command operation is performed every month after the constraints of other fields are met.

· Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"

· Medium Bar (-): You can use the middle bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"

· Forward slash (/): You can use a forward slash to specify the interval frequency of the time, for example, "0-23/2" means to perform every two hours. The forward slash can also be used with the asterisk, such as */10, which, if used in the minute field, means that it is executed every 10 minutes.


5. Reload crontab configuration: Service Crond Reload problems encountered:

Problem Description: Sh file, in win environment, with WINSCP Edit, the following error occurred:

-bash:./start.sh:/bin/sh^m:bad interpreter:no such file or directory

Solution: This is the result of Win's code, which can be resolved as follows.

1. View the file: Vim monitor.sh

2. View the format of the error file (general error file format is Dos):

: Set FF

3. Modify the file format to UNIX:

: Set Ff=unix

4. Save again.

: wq!

@ If there are questions, you can DMS to me, you can discuss each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.