WEB應用監控與自動重啓的程序和腳本

 首先是在root用戶的crontab中添加如下一行:

         */5 * * * * /xxx/xxx/monitorM2U.sh

         即每5分鐘執行一下腳本/xxx/xxx/monitorM2U.sh,這個腳本的內容是:

cd /xxx/xxx/xxx/classes;

nohup “${JAVA_HOME}”/bin/java –cp ".:${CLASSPATH} " com.mdcchina.m2u.monitor.ResponseMonitor &

         

 

         其中com.mdcchina.m2u.monitor.ResponseMonitor的代碼是:

 

/*

 * Created on 2005-9-8

 *

 */

package com.mdcchina.m2u.monitor;

 

import java.io.IOException;

import java.util.Date;

 

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.methods.GetMethod;

 

/**

 * @author Hao Wei 監控m2u進程是否能夠訪問,如果不能就重啓之

 */

public class ResponseMonitor {

 

    public static void main(String[] args) {

        if (!new ResponseMonitor()

                . canRequest ("http://www.m2u.cn/albumshare/benber")) { //用來監控的地址

            log("FATAL ERROR!!!!!!!!!!!!!/nM2U cannot be visit, it will be restarted!");

/**

*                 在此可加入發送短信報警或記錄失敗日誌的代碼

**/

            try {

                String script = "/xxx/xxx/restart_tomcat.sh";

                Process proc = Runtime.getRuntime().exec("/bin/sh " + script); //調用腳本進行重啓

                log("execute: " + script);

                log(""+proc.waitFor());

                log("===========M2U process was restarted!====== ======");

            } catch (IOException e) {

                log("M2U cannot be restart!!!!!!!!!!!!/n" + e.getStackTrace());

            }

            catch( InterruptedException e1){

                log("M2U cannot be restart!!!!!!!!!!!!/n" + e1.getStackTrace());

            }

        } else {

            log("M2U OK!");

        }

    }

 

    private static void log(Object obj) {

        System.out.println("::::MONITOR::::|" + new Date() + "|" + obj);

    }

 

    public boolean canRequest(String url) {

        boolean success = false;

        HttpClient client = null;

        GetMethod getMethod = null;

        try {

            client = new HttpClient();

            log("<<<< begin send http request to " + url);

 

            getMethod = new GetMethod(url);

            client.setConnectionTimeout(40 * 1000);

            client.setTimeout(40 * 1000);

            client.setHttpConnectionFactoryTimeout(40 * 1000);

            client.executeMethod(getMethod);

            log("response code : " + getMethod.getStatusCode());

            if (getMethod.getStatusCode() < 300) {

                success = true;

            }

        } catch (IOException ex) {

            log("ERROR/n" + ex.getStackTrace());

            success = false;

        } finally {

            try {

                if (getMethod != null) {

                    getMethod.releaseConnection();

                }

            } catch (Exception ex2) {

            }

            try {

                if (client != null) {

                    client.endSession();

                }

            } catch (Exception ex1) {

            }

            client = null;

        }

        log("<<<< end send http request to " + url);

        return success;

    }

}

 

其中,這個java程序調用的重啓tomcat的腳本/xxx/xxx/restart_tomcat.sh內容如下:

/xxx/xxx/get_tomcat_pid.sh | while read PID

do

kill -9 "${PID}" &

done

su - mytt -c "cd ${CATALINA_HOME}/bin; ./startup.sh"

 

其中,找到tomcat主進程號的腳本/xxx/xxx/get_tomcat_pid.sh內容如下:

ps -efww|grep tomcat | while read PROCESS

do

PID2=`echo "${PROCESS}" | awk '{printf ("%s/n",$3)}'`

         if [ "${PID2}" == "1" ]; then

                 PID1=`echo "${PROCESS}" | awk '{printf ("%s/n",$2)}'`

                        echo "${PID1}"

         else

                 continue

         fi

done

 

完鳥。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章