java监控守护linux程序运行

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liuxihope/article/details/84757543
在系统运行过程中,出现系统进程退出,整个消失的情况。使用JAVA程序监控并守护其运行。


public void doTask_activemq() throws InterruptedException {
/**activemq**/
try {
Process process1 = Runtime.getRuntime().exec("ps -ef|grep java |grep activemq");

String result = getProcessResult(process1);

if (StringUtils.isBlank(result)
|| !result.contains("/home/abc/apache-activemq-5.11.1/bin/activemq.jar")) {
logger.info("@@@activemq进程挂了,重启activemq...");
Runtime.getRuntime().exec("/home/abc/apache-activemq-5.11.1/bin/activemq start");
} else {
logger.info("activemq进程正常运行...");
}
} catch (IOException e) {
e.printStackTrace();
}

}


private String getProcessResult(Process process1) throws IOException, InterruptedException {
process1.waitFor();

InputStream is = process1.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
reader.close();
process1.destroy();

return sb.toString();
}

展开阅读全文

没有更多推荐了,返回首页