Linux环境下Java操控Tomcat、Apache自动重启
来源:互联网 发布:重阳节算法定节假日吗 编辑:程序博客网 时间:2024/04/30 02:06
- //软件安装目录
- 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();
- }
0 0
- Linux环境下Java操控Tomcat、Apache自动重启
- Linux下tomcat服务自动重启
- jenkins重启linux环境下 tomcat
- Linux 下用Java重启Tomcat
- Linux下定时自动重启apache及mysql
- linux下实现tomcat定时自动重启
- linux下定时自动重启tomcat服务器
- Linux下使用shell脚本自动监控重启tomcat
- Linux下实现tomcat定时自动重启
- Linux下使用shell脚本自动监控重启tomcat
- linux下配制tomcat自动重起
- Linux环境下Tomcat的重启(markdown)
- linux环境下Apache+Tomcat集群配置
- Linux部署tomcat+apache+java环境
- Windows下设置tomcat自动重启
- linux 自动重启tomcat 脚本
- linux--tomcat自动重启--shell编程
- linux自动重启tomcat脚本分享
- 3. SSH 连接、远程上传下载文件
- 盗版XP成主要恶意攻击对象
- Android 全局异常处理
- 开发笔记之20140127
- 名字的由来
- Linux环境下Java操控Tomcat、Apache自动重启
- year by year
- Spring MVC 图片上传
- Javascript入门基本语法
- 盗版XP成主要恶意攻击对象
- 基于cell-base的NSTableView
- UIResponder
- 五招防止QQ密码被盗
- Github如何提交代码