实时监控进程process并启动的工具类

    public static void monitor(String EXEName, String EXEPath) {
        String query_cmd = "wmic process get name"; // 查询当前运行的进程信息
        String res = "";
        BufferedReader bReader = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(query_cmd);
            process.getOutputStream().close();
            bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = bReader.readLine()) != null) {
                res += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.info(e.getMessage());
        } finally {
            if (process != null)
                process.destroy();
        }
        log.debug(res);
        // 如果当前进程列表中不包含
        if (res.indexOf(EXEName) == -1) {
            try {
                Runtime.getRuntime().exec(EXEPath);
                log.info("启动服务:" + EXEName);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("启动服务失败:" + e.getMessage());
            }
        } else {
            log.info("服务正常运行");
        }
    }

猜你喜欢

转载自blog.csdn.net/Peter_Changyb/article/details/81629478