Spawn a process in Java that survives a JVM shutdown

后端 未结 7 1767
滥情空心
滥情空心 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 03:45

    It won't always "just work". When JVM spawns the child and then shuts down, the child process will also shutdown in some cases. That is expected behaviour of the process. Under WIN32 systems, it just works.

    E.g. If WebLogic server was started up by a Java process, and then that process exits, it also sends the shutdown signal to the WebLogic via shutdown hook in JVM, which causes WebLogic to also shutdown.

    If it "just works" for you then there is no problem, however if you find yourself in a position that child process also shutsdown with JVM it is worth having a look at the "nohup" command. The process won't respond to SIGTERM signal, but will respond to SIGKILL signal, as well as normal operations.

    Update: The way described above is a bit of an overkill. Another way of doing this would be to use "&" on the end of command. This will spawn a new process that is not a child of current java process.

    P.S. Sorry for so many updates, I have been learning and trying it from scratch.

    0 讨论(0)
  • 2020-12-10 03:56

    It does actually "just work", unless you're trying to be clever.

    My wrapped java.lang.Process was trying to capture the script's output, so when the JVM died, the script didn't have anywhere to send output so it just dies. If I don't try to capture the output, or the script doesn't generate any or redirects everything to a file or /dev/null, everything works as it should.

    0 讨论(0)
  • 2020-12-10 03:57

    I thought the whole point of Java was that it's fully contained within the JVM. It's kinda hard to run bytecode when there's no runtime.

    If you're looking to have a totally separate process you might look into trying to start a second java.exe instance. Although for your application, it might be easier to simply make a synchronized block that stops (but doesn't kill) your app, does the updating, and then re-initializes your app's data.

    0 讨论(0)
  • 2020-12-10 04:00

    If you're spawning the process using java.lang.Process it should "just work" - I don't believe the spawned process will die when the JVM exits. You might find that the Ant libraries make it easier for you to control the spawning though.

    0 讨论(0)
  • 2020-12-10 04:01

    >>don't believe the spawned process will die when the JVM exits.

    Child process is always dying on my box(SuSE) whenever I kill java. I think, the child process will die if it's dealing with I/O of the parent process(i.e., java)

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题