`
lupingui
  • 浏览: 157349 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Linux环境下Java操控Tomcat、Apache自动重启

阅读更多

 

	//软件安装目录
	static final String TOMCAT_DIR = "/usr/local/tomcat/";
	static final String APACHE_DIR = "/usr/local/apache2/";
	//重试次数
	static final int RETRY_TIME = 10;
	
	/**
	 * 重启Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午05:27:24
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean restartTomcat(Runtime runtime) throws IOException{
		//结束tomcat进程
		for (int i = 0; i < RETRY_TIME; i++) {
			if(isExistTomcatProcess(runtime)) {
				//调用tomcat自身脚本结束进程
				shutdownTomcat(runtime);
				try {
					Thread.currentThread().sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(isExistTomcatProcess(runtime)) {	
					//强制结束进程
					killTomcatBySoft(runtime);
					try {
						Thread.currentThread().sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}else {
				break;
			}
		}		
		//启动tomcat
		for (int i = 0; i < RETRY_TIME; i++) {
			if(!isExistTomcatProcess(runtime)) {
				//调用tomcat自身脚本重启程序
				startupTomcat(runtime);
				try {
					Thread.currentThread().sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else {
				break;
			}
		}
		
		if(isExistTomcatProcess(runtime)) {
			return true;
		}else {
			return false;
		}
		
	}
	
	/**
	 * 判断是否含有tomcat进程
	 * @auther lupingui
	 * 2010-1-4 下午04:41:04
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistTomcatProcess(Runtime runtime) throws IOException {
		String program = "org.apache.catalina.startup.Bootstrap start";
		String command = "ps -C java -f --cols=1000";
		return isExistProcess(runtime, command, program);
	}
	
	/**
	 * 判断是否含有apache进程
	 * @auther lupingui
	 * 2010-1-4 下午04:41:14
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistApacheProcess(Runtime runtime) throws IOException {
		String program = APACHE_DIR + "bin/httpd";
		String command = "ps -C httpd -f --cols=1000";
		return isExistProcess(runtime, command, program);
	}
	
	/**
	 * 判断当前进程中是否含有program
	 * @auther lupingui
	 * 2010-1-4 下午04:41:18
	 * @param runtime
	 * @param command
	 * @param program
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistProcess(Runtime runtime, String command, String program) throws IOException {
		boolean isExistTomcatProcess = false;
		
		Process process = runtime.exec(command);
		InputStream in = process.getInputStream();
		InputStreamReader is = new InputStreamReader(in);
		BufferedReader read = new BufferedReader(is);  
		String line = "";  
		while((line = read.readLine()) != null) {
			if(line.indexOf(program) >= 0) {
				isExistTomcatProcess = true;
				break;
			}
		}
		//---------------------
		in.close();
		is.close();
		read.close();
		process.destroy();
		//---------------------
		
		return isExistTomcatProcess;
	}
	
	/**
	 * 关闭Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午04:41:28
	 * @param runtime
	 * @throws IOException
	 */
	public static void shutdownTomcat(Runtime runtime) throws IOException {
		runtime.exec(TOMCAT_DIR + "bin/shutdown.sh");
	}
	
	/**
	 * 启动Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午04:41:56
	 * @param runtime
	 * @throws IOException
	 */
	public static void startupTomcat(Runtime runtime) throws IOException {
		runtime.exec(TOMCAT_DIR + "bin/startup.sh");
	}
	
	/**
	 * 重启Apache
	 * @auther lupingui
	 * 2010-1-4 下午05:30:06
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean restartApache(Runtime runtime) throws IOException {
		if(isExistApacheProcess(runtime)) {
			runtime.exec(APACHE_DIR + "bin/apachectl restart");
		}else {
			runtime.exec(APACHE_DIR + "bin/apachectl start");
		}
		
		if (isExistApacheProcess(runtime)){
			return true;
		}else{
			return false;			
		}
	}
	
	/**
	 * 强制结束Tomcat进程
	 * @auther lupingui
	 * 2010-1-4 下午04:46:34
	 * @param runtime
	 * @throws IOException
	 */
	public static void killTomcatBySoft(Runtime runtime) throws IOException {
		String[] cmd = {"sh", "-c", "ps aux | grep tomcat"};
		Process process = runtime.exec(cmd);
		InputStream in = process.getInputStream();
		InputStreamReader is = new InputStreamReader(in);
		BufferedReader read = new BufferedReader(is);
		String line = null;  
		while((line = read.readLine()) != null) {
			if(line.indexOf("org.apache.catalina.startup.Bootstrap start") >= 0) {
				String tomcatPid = line.split("\\s+")[1];
				runtime.exec("kill -9 "+tomcatPid);
			}
		}
		in.close();
		is.close();
		read.close();
		process.destroy();
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics