Java ProcessBuilder is not able to start GNU screen

Viewed 112

I'm experiencing a weird behaviour of java's ProcessBuilder. What I try is to stop a running screen using a shell, delete a few folders and after that restart the screen using another shell script. The first step, killing the running screen, runs perfectly using:

 ProcessBuilder pb0 = new ProcessBuilder(System.getProperty("user.dir") + "/generator/stop.sh");

In this stop.sh shell I simply run

screen -X -S generator kill

which works as it should. After that I delete my directorys using org.apache.commons.io.FileUtils and then I want to start the screen again. Currently I'm doing it like that:

    System.out.println("Restarting the generator");

    ProcessBuilder pb1 = new ProcessBuilder();
    pb1.directory(new File(System.getProperty("user.dir") + "/generator"));
    pb1.command("./start.sh");
    try {
        Process process = pb1.start();
        System.out.printf("Started the generator with %d", process.waitFor());
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }

This gives out "Started the generator with 1" which indicates to me that the screen runs which is simply not the case when checking with screen -ls. No errors, no clue on how to move forward from here

Inside start.sh:

screen -S generator java -Xms2G -Xmx2G -jar generator.jar

PS: I'm using Debian 10. Maybe anyone can help me out here? Greets!

1 Answers

You should never ignore process output, because it's buffer has limited length, and if you don't consume it, it will hang. Im not sure if this is causing your issue, but this is definitely something you should do.

It is also possible that process throws some error that you can't see because you are ignoring it's output (in which case, this will help you to investigate the issue).

Try this:

new ProcessBuilder()
  .redirectOutput(ProcessBuilder.Redirect.INHERIT)
  .redirectError(ProcessBuilder.Redirect.INHERIT)
  ...

This will redirect process output stream and input stream to it's parent (which is your application).

Related