欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java监控守护linux程序运行

程序员文章站 2022-03-26 13:30:46
...
在系统运行过程中,出现系统进程退出,整个消失的情况。使用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();
	}