Use Shell to automatically monitor tomcat and perform restart operations under LINUX

The file name is monitor.sh, if you don't have execute permission, please use chmod 777 monitor.sh

 

 

#!/bin/sh

 

# func: Automatically monitor tomcat scripts and perform restart operations

# author:danny

# date:02/20/2013

# DEFINE

 

# Get tomcat process ID

TomcatID = $ (ps -ef | grep tomcat | grep -w 'dw \ /apache-tomcat-7.0.34 \ / conf' | grep -v 'grep' | awk '{print $ 2}')

 

# tomcat startup program (note the actual installation path of tomcat here)

StartTomcat=/home/dw/apache-tomcat-7.0.34/bin/startup.sh

#TomcatCache=/usr/apache-tomcat-5.5.23/work

 

# Define the page address to monitor

WebUrl = http: //haoyayi.eyar.com

 

# log output

GetPageInfo=/dev/null

TomcatMonitorLog=/tmp/TomcatMonitor.log

 

Monitor()

{

  echo "[info]Start monitoring tomcat...[$(date +'%F %H:%M:%S')]"

  if [[ $TomcatID ]];then # here to determine whether the TOMCAT process exists

    echo "[info]The current tomcat process ID is: $TomcatID, continue to detect the page..."

    # Check whether the startup is successful (if successful, the page will return the status "200")

    TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})

    if [ $TomcatServiceCode -eq 200 ];then

        echo "[info] page return code is $TomcatServiceCode, tomcat is successfully started, and the test page is normal..."

    else

        echo "[error]tomcat page error, please note... the status code is $TomcatServiceCode, the error log has been output to $GetPageInfo"

        echo "[error] page access error, start to restart tomcat"

        #kill -9 $TomcatID # Kill the original tomcat process

        #sleep 3

        #rm -rf $TomcatCache # Clean up tomcat cache

        #$StartTomcat

    be

  else

    echo "[error]tomcat process does not exist! tomcat starts to restart automatically..."

    echo "[info]$StartTomcat, please wait..."

    #rm -rf $TomcatCache

    #$StartTomcat

  be

  echo "------------------------------"

}

Monitor>>$TomcatMonitorLog

 

 

The following code is to monitor whether tomcat is running, and if it is running, it will be stopped. The name of the file itself is shutdown.sh

 

 

#!/bin/sh

TomcatID = $ (ps -ef | grep hudson-3.01 | java grep | awk '{print $ 2}')

echo "The pid of tomcat is $TomcatID"

Monitor(){

        echo "[info]Start monitoring tomcat...[$(date +'%F %H:%M:%S')]"

        if [[ $TomcatID ]]

# Here to determine whether the TOMCAT process exists

           then

                echo "tomca starts normally"

                kill -9 $TomcatID

                tempTomcatID=$(ps -ef |grep hudson-3.01 |grep java | awk ' { print $2 } ')

                if [[ $tempTomcatID ]]

                        then

                        echo "stop failed"

                else 

                        echo "Successfully stopped"

                be

        else

            echo "tomcat is not started"

        be

}

 

 

Monitor

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326332798&siteId=291194637