public class Daemon extends Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
Daemon.WithoutChdir
Flavor of
Daemon that doesn't change the current directory. |
Constructor and Description |
---|
Daemon() |
Modifier and Type | Method and 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 String |
getCurrentExecutable()
Gets the current executable name.
|
void |
init()
Prepares the current process to act as a daemon.
|
void |
init(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(String pidFile)
Writes out the PID of the current process to the specified file.
|
public void all(boolean daemonize) throws Exception
daemonize
- Parse the command line arguments and if the application should be
daemonized, pass in true.Exception
public boolean isDaemonized()
daemonize()
.public void daemonize() throws IOException
IOException
public void daemonize(JavaVMArguments args)
public static void selfExec(JavaVMArguments args)
public void init() throws Exception
/var/run/daemon.pid
.Exception
public void init(String pidFile) throws Exception
pidFile
- the filename to which the daemon's PID is written;
or, null
to skip writing a PID file.Exception
protected void closeDescriptors() throws IOException
This method can be overridden to no-op in a subtype. Useful for debugging daemon processes when they don't work correctly.
IOException
protected void chdirToRoot()
protected void writePidFile(String pidFile) throws IOException
pidFile
- the filename to write the PID to.IOException
public static String getCurrentExecutable()
Copyright © 2015. All rights reserved.