您的位置:首页 > 运维架构 > Shell

java调用shell的一段代码

2013-07-15 16:48 295 查看
private final static String FILL_EVENT_SCRIPT = "/opt/soft/kettle/run/run_report.sh";   //shell脚本的路径位置
private void processEvent(String reportName, String context) throws Exception {
        logger.info("reportName: " + reportName + " | context: " + context);
        File f = new File(FILL_EVENT_SCRIPT);
        if (f.exists()) {
            String[] cmd = { FILL_EVENT_SCRIPT, "-c", reportName, context};
           
Process process = Runtime.getRuntime().exec(cmd);
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line = null;
            process.waitFor();
            while ((line = input.readLine()) != null) {
             logger.info(line);
            }
            if (ir != null) {
                ir.close();
            }
            if (input != null) {
                input.close();
            }
        } else {
            logger.info("file is not exists");
        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息