Package com.sun.akuma

Class Daemon

java.lang.Object
com.sun.akuma.Daemon
Direct Known Subclasses:
Daemon.WithoutChdir, NetworkServer

public class Daemon extends 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 
    Flavor of Daemon that doesn't change the current directory.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    all(boolean daemonize)
    Do all the necessary steps in one go.
    protected void
    change directory to '/' to avoid locking directories.
    protected void
    Closes inherited file descriptors.
    void
    Relaunches the JVM with the exact same arguments into the daemon.
    void
    Relaunches the JVM with the given arguments into the daemon.
    static String
    Gets the current executable name.
    void
    Prepares the current process to act as a daemon.
    void
    init(String pidFile)
    Prepares the current process to act as a daemon.
    boolean
    Returns true if the current process is already launched as a daemon via daemonize().
    static void
    Overwrites the current process with a new Java VM with the given JVM arguments.
    protected void
    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 Details

    • Daemon

      public Daemon()
  • Method Details

    • all

      public void all(boolean daemonize) throws 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:
      Exception
    • isDaemonized

      public boolean isDaemonized()
      Returns true if the current process is already launched as a daemon via daemonize().
    • daemonize

      public void daemonize() throws IOException
      Relaunches the JVM with the exact same arguments into the daemon.
      Throws:
      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 Exception
      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

      public void init(String pidFile) throws 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:
      Exception
    • closeDescriptors

      protected void closeDescriptors() throws 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:
      IOException
    • chdirToRoot

      protected void chdirToRoot()
      change directory to '/' to avoid locking directories.
    • writePidFile

      protected void writePidFile(String pidFile) throws IOException
      Writes out the PID of the current process to the specified file.
      Parameters:
      pidFile - the filename to write the PID to.
      Throws:
      IOException
    • getCurrentExecutable

      public static String getCurrentExecutable()
      Gets the current executable name.