zoukankan      html  css  js  c++  java
  • java.lang.Process启动Linux脚本方法

    package com.eastcom_sw.stream.webservices.controller;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.eastcom_sw.common.web.BaseController;
    
    @Controller
    @RequestMapping(value = "/reenDataController")
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class ReenDataController extends BaseController {
        /**
         * <p>Description:重启tomcat </p>
         * <p>Title: resetTomcat</p>
         * @param session
         * @param request
         * @param response
         */
        @RequestMapping(value="/resetTomcat")
        public void resetTomcat(HttpSession session, HttpServletRequest request, 
                HttpServletResponse response){
            String message="";
            BufferedReader br= null;
            Process process = null;  
            try {
                 String shPath= "/home/tomcat_ws/bin/start_tomcatws.sh";   //tomecat启动脚本路径
                 System.out.println("=============启动脚本路径:"+shPath);  
                 process = Runtime.getRuntime().exec(shPath.toString());  
                 br = new BufferedReader(new InputStreamReader(process.getInputStream()));  
                StringBuffer sb = new StringBuffer();  
                String line;  
                while ((line = br.readLine()) != null) {  
                    sb.append(line).append("
    ");  
                }  
                String result = sb.toString();  
                System.out.println("=============启动脚本-执行结果:"+result);  
                if(process != null){  
                    int extValue = process.waitFor(); //返回码 0 表示正常退出 1表示异常退出  
                    if(0 == extValue){  
                        System.out.println("=============启动脚本-执行完毕!"); 
                    }else{ 
                        System.out.println("=============启动脚本-执行异常!"); 
                        message="启动脚本执行异常";
                    }
                }  
                 this.addResultInfo(SUCCESS,message,"");
            }catch (IOException e) {
                // TODO Auto-generated catch block
                message= "启动脚本执行异常";
                e.printStackTrace();
                this.addResultInfo(FAILURE, message, "");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                message= "启动脚本异常退出 ";
                e.printStackTrace();
                this.addResultInfo(FAILURE, message, "");
            }finally {  
                try {  
                    br.close();  
                    process.destroy();  
                } catch (Exception e) {  
                    log.error("流或process销毁异常",e.getMessage());  
                }  
            }  
            this.responseResult(response, this.getResultJSONStr());
        }
    }
  • 相关阅读:
    在win7中关闭UAC(用户存取控制)
    如何卸载那些没有出现在“add/remove program”中的程序?
    Win7 打开显示文件后缀
    有关windows 自动登录的registry Key
    原来如此 新学一招
    file's owner以及outlet与连线的理解
    学习笔记:正确使用PresentModalViewController
    学习笔记:UIViewController生命周期
    IPhone之自定义弹出窗口
    学习笔记:iOS 视图控制器(UIViewController)剖析
  • 原文地址:https://www.cnblogs.com/baixiuhua/p/9132205.html
Copyright © 2011-2022 走看看