Spawn a process in Java that survives a JVM shutdown

后端 未结 7 1774
滥情空心
滥情空心 2020-12-10 03:40

I need to spawn a process in Java (under Linux exclusively) that will continue to run after the JVM has exited. How can I do this?

Basically the Java app should sp

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 04:02

    I was having trouble with this and the launched process was getting killed when the JVM shutdown.

    Redirecting stdout and stderr to a file fixed the issue. I guess the process was tied to the launched java app as by default it was expecting to pass its output to it.

    Here's the code that worked for me (minus exception handling):

    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.redirectOutput(logFile);
    pb.redirectError(logFile);
    Process p = pb.start();
    

提交回复
热议问题