|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sun.akuma.Daemon
public class Daemon
Forks a copy of the current process into the background.
Because of the fork/exec involved in doing this, your code has to call Daemonizer in a certain sequence. Specifically, from your main method:
public static void main(String[] args) { Daemon d = new Daemon(); if(d.isDaemonized()) { // perform initialization as a daemon // this involves in closing file descriptors, recording PIDs, etc. d.init(); } else { // if you are already daemonized, no point in daemonizing yourself again, // so do this only when you aren't daemonizing. if(you decide to launch a copy into background) { d.daemonize(...); System.exit(0); } } // your normal main code follows // this part can be executed in two ways // 1) the user runs your process in the foreground // 2) you decided to daemonize yourself, in which case the newly forked daemon will execute this code, // while the originally executed foreground Java process exits before it gets here. ... }
Alternatively, your main class can extend from Daemon, so that you can customize some of the behaviors.
Nested Class Summary | |
---|---|
static class |
Daemon.WithoutChdir
Flavor of Daemon that doesn't change the current directory. |
Constructor Summary | |
---|---|
Daemon()
|
Method Summary | |
---|---|
void |
all(boolean daemonize)
Do all the necessary steps in one go. |
protected void |
chdirToRoot()
change directory to '/' to avoid locking directories. |
protected void |
closeDescriptors()
Closes inherited file descriptors. |
void |
daemonize()
Relaunches the JVM with the exact same arguments into the daemon. |
void |
daemonize(JavaVMArguments args)
Relaunches the JVM with the given arguments into the daemon. |
static java.lang.String |
getCurrentExecutable()
Gets the current executable name. |
void |
init()
Prepares the current process to act as a daemon. |
void |
init(java.lang.String pidFile)
Prepares the current process to act as a daemon. |
boolean |
isDaemonized()
Returns true if the current process is already launched as a daemon via daemonize() . |
static void |
selfExec(JavaVMArguments args)
Overwrites the current process with a new Java VM with the given JVM arguments. |
protected void |
writePidFile(java.lang.String pidFile)
Writes out the PID of the current process to the specified file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Daemon()
Method Detail |
---|
public void all(boolean daemonize) throws java.lang.Exception
daemonize
- Parse the command line arguments and if the application should be
daemonized, pass in true.
java.lang.Exception
public boolean isDaemonized()
daemonize()
.
public void daemonize() throws java.io.IOException
java.io.IOException
public void daemonize(JavaVMArguments args)
public static void selfExec(JavaVMArguments args)
public void init() throws java.lang.Exception
/var/run/daemon.pid
.
java.lang.Exception
public void init(java.lang.String pidFile) throws java.lang.Exception
pidFile
- the filename to which the daemon's PID is written;
or, null
to skip writing a PID file.
java.lang.Exception
protected void closeDescriptors() throws java.io.IOException
This method can be overridden to no-op in a subtype. Useful for debugging daemon processes when they don't work correctly.
java.io.IOException
protected void chdirToRoot()
protected void writePidFile(java.lang.String pidFile) throws java.io.IOException
pidFile
- the filename to write the PID to.
java.io.IOException
public static java.lang.String getCurrentExecutable()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |