tags:

views:

2807

answers:

5

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 spawn an updater which stops the Java app, updates files and then starts it again.

I'm interested in a hack & slash method to just get it working as well as a better design proposal if you have one :)

+7  A: 

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.

Jon Skeet
And since Java 5, there is http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html to assist in creating processes
toolkit
+3  A: 

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.

A: 

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.

Dashogun
A: 

If you're looking at making an updater on Linux, you're probably barking up the wrong tree. I believe all major linux distros have a package manager built in. You should use the package manager to do your updating. Nothing frustrates me more than programs that try to self-update... (I'm looking at you, Eclipse)

rmeador
But different distros use completely different mechanisms (rpm vs. apt-get vs. ?). Is he supposed to package the same Java app up multiple different ways just to accomodate that quirk when he can actually handle the download and everything except the final move or copy internally on any platform.If this wasn't a Java app, I'd agree with you, in this case I do not.
John Munsch
A: 

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.