如何从Java ProcessBuilder获取shell脚本进程构建的进程ID

时间:2018-05-04 08:34:50

标签: java bash shell java-ee scripting

如何获取使用ProcessBuilder启动的shell脚本的进程ID?

String cmd[] = { "sh", "-c", "ls -l" };
        ProcessBuilder pb = new ProcessBuilder(cmd);
        pb.redirectOutput(new File(request.getParameter("output_file_name")));
        Process p = pb.start();
        try {
            p.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        p.destroy();

1 个答案:

答案 0 :(得分:-1)

请参阅:How can a Java program get its own process ID?

Java 9现在支持这一点。否则,从您的应用程序中调出脚本以保存您将在某处读取的PID