Package com.sun.akuma
Class Daemon
java.lang.Object
com.sun.akuma.Daemon
- Direct Known Subclasses:
Daemon.WithoutChdir,NetworkServer
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.
- Author:
- Kohsuke Kawaguchi
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFlavor ofDaemonthat doesn't change the current directory. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidall(boolean daemonize) Do all the necessary steps in one go.protected voidchange directory to '/' to avoid locking directories.protected voidCloses inherited file descriptors.voidRelaunches the JVM with the exact same arguments into the daemon.voiddaemonize(JavaVMArguments args) Relaunches the JVM with the given arguments into the daemon.static StringGets the current executable name.voidinit()Prepares the current process to act as a daemon.voidPrepares the current process to act as a daemon.booleanReturns true if the current process is already launched as a daemon viadaemonize().static voidselfExec(JavaVMArguments args) Overwrites the current process with a new Java VM with the given JVM arguments.protected voidwritePidFile(String pidFile) Writes out the PID of the current process to the specified file.
-
Constructor Details
-
Daemon
public Daemon()
-
-
Method Details
-
all
Do all the necessary steps in one go.- Parameters:
daemonize- Parse the command line arguments and if the application should be daemonized, pass in true.- Throws:
Exception
-
isDaemonized
public boolean isDaemonized()Returns true if the current process is already launched as a daemon viadaemonize(). -
daemonize
Relaunches the JVM with the exact same arguments into the daemon.- Throws:
IOException
-
daemonize
Relaunches the JVM with the given arguments into the daemon. -
selfExec
Overwrites the current process with a new Java VM with the given JVM arguments. -
init
Prepares the current process to act as a daemon. The daemon's PID is written to the file/var/run/daemon.pid.- Throws:
Exception
-
init
Prepares the current process to act as a daemon.- Parameters:
pidFile- the filename to which the daemon's PID is written; or,nullto skip writing a PID file.- Throws:
Exception
-
closeDescriptors
Closes inherited file descriptors.This method can be overridden to no-op in a subtype. Useful for debugging daemon processes when they don't work correctly.
- Throws:
IOException
-
chdirToRoot
protected void chdirToRoot()change directory to '/' to avoid locking directories. -
writePidFile
Writes out the PID of the current process to the specified file.- Parameters:
pidFile- the filename to write the PID to.- Throws:
IOException
-
getCurrentExecutable
Gets the current executable name.
-