WEB应用监控与自动重启的程序和脚本
来源:互联网 发布:矩阵管理制度 编辑:程序博客网 时间:2024/04/28 04:51
*/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
完鸟。
- WEB应用监控与自动重启的程序和脚本
- 用shell脚本实现监控程序自动重启
- 用shell脚本实现监控程序自动重启
- 用shell脚本实现监控程序自动重启
- 用shell脚本实现监控程序自动重启
- IIS监控--自动重启脚本工具
- IIS监控--自动重启脚本工具
- shell自动监控重启Tomcat脚本
- shell自动监控重启Tomcat脚本
- shell自动监控重启Tomcat脚本
- shell自动监控重启Tomcat脚本
- 程序自动重启脚本
- 监控服务器程序,挂掉后自动重启
- 程序崩掉后 自动重启的shell脚本
- linux 进程监控和自动重启的简单实现
- linux 进程监控和自动重启的简单实现
- linux 进程监控和自动重启的简单实现 .
- linux 进程监控和自动重启的简单实现
- asp.net分页控页(vb.net)源码
- 人应该明白的五句话
- 详细介绍用JBuilder9开发的Struts实例
- 找工作難 , 難與上親天
- 【中国的悲哀,教育的悲哀】
- WEB应用监控与自动重启的程序和脚本
- C#基础二十五问
- 男女有别之一句话经典
- sql分页存储过程!与asp.net分页控件一起使用
- 一个css横向菜单的一点经验
- 女人宣言:谁有资格婚外恋 (ZT好文推荐)
- 经纬度距离计算
- ASP 编译成DLL ,步骤说明
- 平凡人的幸福