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());
        }
    }
  • 相关阅读:
    AOP AspectJ 字节码 语法 MD
    判断小米华为等系统 MD
    vuejs2.0实现分页组件,使用$emit进行事件监听数据传递
    vuejs2.0实现一个简单的分页
    vuejs2.0使用Sortable.js实现的拖拽功能
    JavaScript之Number、String、Array常用属性与方法手册
    CSS3效果:5种预载动画效果
    vuejs 1.x
    window.requestAnimationFrame与Tween.js配合使用实现动画缓动效果
    如何用JavaScript判断dom是否有存在某class的值?
  • 原文地址:https://www.cnblogs.com/baixiuhua/p/9132205.html
Copyright © 2011-2022 走看看