Posted by kohsuke
on January 31, 2009 at 11:40 AM PST
Writing a proper Unix daemon in Java used to be very difficult, due to the lack of access to the OS capabilities from Java. So I wrote a library to overcome this problem.
As explained in here , writing a proper daemon requires various function calls that are traditionally only available to native applications. So typically, Java applications rely on external wrappers like Apache commons daemon or Java service wrapper , or even worse, just ignore those conventions and do Runtime.exec.
But this creates an unnecessary inconsistency between how you launch a program in the foreground vs how you launch a program in the background, and hurt the user/administration experience.
To solve this problem, Akuma is provided as a library that can be embedded in your application. After your program starts normally, you can programmaticaly decide whether you want to launch into the background (which could be driven by your application-specific command line switch or something) then call into Akuma to initiate the daemonization.
Behind the scene, Akuma will figure out the exact command line options given to the current process, then use fork/exec combo plus some additional steps to start a daemon. Unlike a real Unix daemon, Java has to call exec after fork, because the forked JVM loses GC threads and HotSpot compiler threads. However, the use of fork/exec allows Akuma to do some interesting things that can never be possible with Runtime.exec, which I'll discuss later.
Head over to the project website for more details and downloads. Currently it supports Linux, Solaris, and Mac OS X. In theory this should also work on FreeBSD, but haven't been tested.