Package com.sun.akuma
Class Daemon
- java.lang.Object
-
- com.sun.akuma.Daemon
-
- Direct Known Subclasses:
Daemon.WithoutChdir
,NetworkServer
public class Daemon extends java.lang.Object
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 Classes Modifier and Type Class Description static class
Daemon.WithoutChdir
Flavor ofDaemon
that doesn't change the current directory.
-
Constructor Summary
Constructors Constructor Description Daemon()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 viadaemonize()
.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.
-
-
-
Method Detail
-
all
public void all(boolean daemonize) throws java.lang.Exception
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:
java.lang.Exception
-
isDaemonized
public boolean isDaemonized()
Returns true if the current process is already launched as a daemon viadaemonize()
.
-
daemonize
public void daemonize() throws java.io.IOException
Relaunches the JVM with the exact same arguments into the daemon.- Throws:
java.io.IOException
-
daemonize
public void daemonize(JavaVMArguments args)
Relaunches the JVM with the given arguments into the daemon.
-
selfExec
public static void selfExec(JavaVMArguments args)
Overwrites the current process with a new Java VM with the given JVM arguments.
-
init
public void init() throws java.lang.Exception
Prepares the current process to act as a daemon. The daemon's PID is written to the file/var/run/daemon.pid
.- Throws:
java.lang.Exception
-
init
public void init(java.lang.String pidFile) throws java.lang.Exception
Prepares the current process to act as a daemon.- Parameters:
pidFile
- the filename to which the daemon's PID is written; or,null
to skip writing a PID file.- Throws:
java.lang.Exception
-
closeDescriptors
protected void closeDescriptors() throws java.io.IOException
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:
java.io.IOException
-
chdirToRoot
protected void chdirToRoot()
change directory to '/' to avoid locking directories.
-
writePidFile
protected void writePidFile(java.lang.String pidFile) throws java.io.IOException
Writes out the PID of the current process to the specified file.- Parameters:
pidFile
- the filename to write the PID to.- Throws:
java.io.IOException
-
getCurrentExecutable
public static java.lang.String getCurrentExecutable()
Gets the current executable name.
-
-