java 调用 linux shell 脚本,并读取返回值

String[] cmd = {"/bin/sh", "-c", "ps -elf | grep -v grep | grep EuropeStaticsEntrance | wc -l"};
while (true) {
    StringBuilder sb = new StringBuilder();
    Process process1 = null;
    process1 = Runtime.getRuntime().exec(cmd);
    BufferedReader input = new BufferedReader(new InputStreamReader(process1.getInputStream()));
    String line = "";
    while ((line = input.readLine()) != null) {
        sb.append(line);
    }
    input.close();
    LOG.info(sb.toString().trim());
    if ("0".equals(sb.toString().trim())) {
        LOG.info("0".equals(sb.toString().trim()));
        break;
    }
    LOG.info("5分钟后后再次检测");
    Thread.sleep(300000);
}

你可能感兴趣的:(java,linux)