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

时间:2021-07-23 07:12:31

How can I get the process id of the shell script started with ProcessBuilder?

如何获取使用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 个解决方案

#1


-1  

See: How can a Java program get its own process ID?

请参阅:Java程序如何获得自己的进程ID?

Java 9 now has support for this. Otherwise call a script out of your application to save the PID which you will read somewhere

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

#1


-1  

See: How can a Java program get its own process ID?

请参阅:Java程序如何获得自己的进程ID?

Java 9 now has support for this. Otherwise call a script out of your application to save the PID which you will read somewhere

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