0

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

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 Answers1

-1

See: How can a Java program get its own process 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

Jacques Koorts
  • 1,819
  • 1
  • 17
  • 10
  • Is it only for Java9, I need to make this with Java6. Is that possible? – Abhishek Gangadhar May 04 '18 at 08:48
  • Can you please elaborate on how you would call a script out of your aplication to save PID – Abhishek Gangadhar May 04 '18 at 08:49
  • First you'd need to know on what operating system you're running. The web is full of examples for each OS. But the script that you're running from ProcessBuilder should include the portion of finding the PID and writing it to file. In windows if you were to call an exe, you would have to call a .bat file that would call the exe and then find the PID for that process. – Jacques Koorts May 04 '18 at 09:00
  • Ya I am using a Linux OS with bash scripts. – Abhishek Gangadhar May 04 '18 at 09:02