Spawn a process in Java that survives a JVM shutdown

后端 未结 7 1778
滥情空心
滥情空心 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.

提交回复
热议问题