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

来源:互联网 发布:重阳节算法定节假日吗 编辑:程序博客网 时间:2024/04/30 02:06
Java代码  收藏代码
  1. //软件安装目录  
  2. static final String TOMCAT_DIR = "/usr/local/tomcat/";  
  3. static final String APACHE_DIR = "/usr/local/apache2/";  
  4. //重试次数  
  5. static final int RETRY_TIME = 10;  
  6.   
  7. /** 
  8.  * 重启Tomcat 
  9.  * @auther lupingui 
  10.  * 2010-1-4 下午05:27:24 
  11.  * @param runtime 
  12.  * @return 
  13.  * @throws IOException 
  14.  */  
  15. public static boolean restartTomcat(Runtime runtime) throws IOException{  
  16.     //结束tomcat进程  
  17.     for (int i = 0; i < RETRY_TIME; i++) {  
  18.         if(isExistTomcatProcess(runtime)) {  
  19.             //调用tomcat自身脚本结束进程  
  20.             shutdownTomcat(runtime);  
  21.             try {  
  22.                 Thread.currentThread().sleep(5000);  
  23.             } catch (InterruptedException e) {  
  24.                 e.printStackTrace();  
  25.             }  
  26.             if(isExistTomcatProcess(runtime)) {   
  27.                 //强制结束进程  
  28.                 killTomcatBySoft(runtime);  
  29.                 try {  
  30.                     Thread.currentThread().sleep(5000);  
  31.                 } catch (InterruptedException e) {  
  32.                     e.printStackTrace();  
  33.                 }  
  34.             }  
  35.         }else {  
  36.             break;  
  37.         }  
  38.     }         
  39.     //启动tomcat  
  40.     for (int i = 0; i < RETRY_TIME; i++) {  
  41.         if(!isExistTomcatProcess(runtime)) {  
  42.             //调用tomcat自身脚本重启程序  
  43.             startupTomcat(runtime);  
  44.             try {  
  45.                 Thread.currentThread().sleep(5000);  
  46.             } catch (InterruptedException e) {  
  47.                 e.printStackTrace();  
  48.             }  
  49.         }else {  
  50.             break;  
  51.         }  
  52.     }  
  53.       
  54.     if(isExistTomcatProcess(runtime)) {  
  55.         return true;  
  56.     }else {  
  57.         return false;  
  58.     }  
  59.       
  60. }  
  61.   
  62. /** 
  63.  * 判断是否含有tomcat进程 
  64.  * @auther lupingui 
  65.  * 2010-1-4 下午04:41:04 
  66.  * @param runtime 
  67.  * @return 
  68.  * @throws IOException 
  69.  */  
  70. public static boolean isExistTomcatProcess(Runtime runtime) throws IOException {  
  71.     String program = "org.apache.catalina.startup.Bootstrap start";  
  72.     String command = "ps -C java -f --cols=1000";  
  73.     return isExistProcess(runtime, command, program);  
  74. }  
  75.   
  76. /** 
  77.  * 判断是否含有apache进程 
  78.  * @auther lupingui 
  79.  * 2010-1-4 下午04:41:14 
  80.  * @param runtime 
  81.  * @return 
  82.  * @throws IOException 
  83.  */  
  84. public static boolean isExistApacheProcess(Runtime runtime) throws IOException {  
  85.     String program = APACHE_DIR + "bin/httpd";  
  86.     String command = "ps -C httpd -f --cols=1000";  
  87.     return isExistProcess(runtime, command, program);  
  88. }  
  89.   
  90. /** 
  91.  * 判断当前进程中是否含有program 
  92.  * @auther lupingui 
  93.  * 2010-1-4 下午04:41:18 
  94.  * @param runtime 
  95.  * @param command 
  96.  * @param program 
  97.  * @return 
  98.  * @throws IOException 
  99.  */  
  100. public static boolean isExistProcess(Runtime runtime, String command, String program) throws IOException {  
  101.     boolean isExistTomcatProcess = false;  
  102.       
  103.     Process process = runtime.exec(command);  
  104.     InputStream in = process.getInputStream();  
  105.     InputStreamReader is = new InputStreamReader(in);  
  106.     BufferedReader read = new BufferedReader(is);    
  107.     String line = "";    
  108.     while((line = read.readLine()) != null) {  
  109.         if(line.indexOf(program) >= 0) {  
  110.             isExistTomcatProcess = true;  
  111.             break;  
  112.         }  
  113.     }  
  114.     //---------------------  
  115.     in.close();  
  116.     is.close();  
  117.     read.close();  
  118.     process.destroy();  
  119.     //---------------------  
  120.       
  121.     return isExistTomcatProcess;  
  122. }  
  123.   
  124. /** 
  125.  * 关闭Tomcat 
  126.  * @auther lupingui 
  127.  * 2010-1-4 下午04:41:28 
  128.  * @param runtime 
  129.  * @throws IOException 
  130.  */  
  131. public static void shutdownTomcat(Runtime runtime) throws IOException {  
  132.     runtime.exec(TOMCAT_DIR + "bin/shutdown.sh");  
  133. }  
  134.   
  135. /** 
  136.  * 启动Tomcat 
  137.  * @auther lupingui 
  138.  * 2010-1-4 下午04:41:56 
  139.  * @param runtime 
  140.  * @throws IOException 
  141.  */  
  142. public static void startupTomcat(Runtime runtime) throws IOException {  
  143.     runtime.exec(TOMCAT_DIR + "bin/startup.sh");  
  144. }  
  145.   
  146. /** 
  147.  * 重启Apache 
  148.  * @auther lupingui 
  149.  * 2010-1-4 下午05:30:06 
  150.  * @param runtime 
  151.  * @return 
  152.  * @throws IOException 
  153.  */  
  154. public static boolean restartApache(Runtime runtime) throws IOException {  
  155.     if(isExistApacheProcess(runtime)) {  
  156.         runtime.exec(APACHE_DIR + "bin/apachectl restart");  
  157.     }else {  
  158.         runtime.exec(APACHE_DIR + "bin/apachectl start");  
  159.     }  
  160.       
  161.     if (isExistApacheProcess(runtime)){  
  162.         return true;  
  163.     }else{  
  164.         return false;             
  165.     }  
  166. }  
  167.   
  168. /** 
  169.  * 强制结束Tomcat进程 
  170.  * @auther lupingui 
  171.  * 2010-1-4 下午04:46:34 
  172.  * @param runtime 
  173.  * @throws IOException 
  174.  */  
  175. public static void killTomcatBySoft(Runtime runtime) throws IOException {  
  176.     String[] cmd = {"sh""-c""ps aux | grep tomcat"};  
  177.     Process process = runtime.exec(cmd);  
  178.     InputStream in = process.getInputStream();  
  179.     InputStreamReader is = new InputStreamReader(in);  
  180.     BufferedReader read = new BufferedReader(is);  
  181.     String line = null;    
  182.     while((line = read.readLine()) != null) {  
  183.         if(line.indexOf("org.apache.catalina.startup.Bootstrap start") >= 0) {  
  184.             String tomcatPid = line.split("\\s+")[1];  
  185.             runtime.exec("kill -9 "+tomcatPid);  
  186.         }  
  187.     }  
  188.     in.close();  
  189.     is.close();  
  190.     read.close();  
  191.     process.destroy();  
  192. }  
0 0
原创粉丝点击