Linux shell Scripts monitor tomcat status and enable auto-start

Source: Internet
Author: User

Recently the company needs to monitor Tomcat's services under Linux, and once the Tomcat service is out of the ordinary or down, restart Tomcat to ensure that the service is up and running, because a shell script under Linux can achieve this effect. Below are the steps for the Linux shell script to monitor the status of Tomcat and to start automatically.

1. Writing Shell Scripts monitor.sh

#!/bin/sh

# func: Automatically monitor tomcat scripts and perform restart operations
# Author:eaglehao
# date:2018-04-08
# DEFINE

# Get Tomcat Process ID (where Tomcat in [grep-w ' Tomcat '] code needs to be replaced with your Tomcat folder name)
tomcatid=$ (ps-ef |grep Tomcat |grep-w ' tomcat ' |grep-v ' grep ' |awk ' {print $} ')

# Tomcat Launcher (note here the path of Tomcat actually installed)
starttomcat=/opt/tomcat/bin/startup.sh
Tomcatcache=/opt/tomcat/work

#定义要监控的页面地址
weburl=http://localhost:8080

#日志输出
Getpageinfo=/tmp/tomcatmonitor.info
Tomcatmonitorlog=/tmp/tomcatmonitor.log

Monitor ()
{
echo "[INFO] starts monitoring tomcat ... [$ (date + '%F%h:%m:%s ')] "
If [$TomcatID];then #这里判断Tomcat进程是否存在
echo "[INFO] current tomcat process ID: $TomcatID, continue to detect page ..."
# Check whether the startup is successful (the page will return 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 OK"
Else
echo "[Error]tomcat page error, please note ... The status code is $tomcatservicecode and 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

2. Test whether the shell script monitor.sh is allowed to succeed

First: Execute the script./monitor. Sh

If you report permission error:chmod 777 monitor.sh

Second: Execute the script./monitor. Sh

If the newspaper/bin/bash^m:, Bad interpreter, no that file or directory: Sed-i ' s/\r$//' build.sh

Third: Execute the script./monitorsh

If \z characters are not output: change Xshell font

3. Add the script to the crontab scheduled task

Check once per minute

Open a/etc/crontab file to add a timed task

Linux shell Scripts monitor tomcat status and enable auto-start

Related Article

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.