在系统运行过程中,出现系统进程退出,整个消失的情况。使用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();
}