2

I have a Java daemon (system service running 24/7/365) that needs to deploy as an executable JAR, however I would like users (on Windows, Linux and Mac alike) to be able to start/stop the application via:

service myapp start

service myapp stop

(Or whatever is the equivalent for Windows/Mac services). Obviously, this requires something at the OS-level to map the myapp "service" to a particular method call from inside my Java app (perhaps, LifecycleManager#start() and LifecycleManager#stop() respectively).

I heard that Apache Commons Daemon can be used for this purpose, and after perusing their site, it looks like it does just this. However it looks like it's an old project and there really isn't any documentation for accomplishing what I am looking for.

So I ask: can commons-daemon do what I need, or do I need something else or in addition? How can I get a cross-platform daemon out of an executable JAR? Thanks in advance!

  • 1
    If you just want start/stop functionality all that you need is for your app to handle SIGTERM gracefully. Try reading some other service scripts to see how they work. – le3th4x0rbot May 05 '13 at 23:02
  • Thanks @BaileyS (+1) - do you have anything Java-specific in mind (articles, tutorial, etc.)? Also, curious about your verbiage here: "If you **just want** starting/stopping functionality...")? Isn't this what `commons-daemon` does (map your Java JAR to service start/stop calls? Or does `commons-daemon` do other stuff too? If so, examples? Thanks again! –  May 05 '13 at 23:07
  • Honestly, I have no idea what commons-daemon does. My point was that if you just want simple startup/shutdown scripts, that you really do not need a special library. The way to handle shutdown is to add a JVM shutdown hook http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29 . Startup is simple, you just call java -jar daemon.jar ... Usually a unix OS will have a blank service script template to help create your own. Good luck! – le3th4x0rbot May 05 '13 at 23:12
  • If you wanted to have a service script that passed a message to the currently running instance, it would be more of an undertaking. An example might be if you wanted to be able to pass a signal to clear a cache without restarting... – le3th4x0rbot May 05 '13 at 23:14

1 Answers1

1

Yes, Apache Commons Daemon can run your jar as a service on Windows (using procrun) or as a daemon on *NIX (using JSVC). I only used it on Windows, so the rest only applies to procrun:

Procrun supports proper Windows service shutdown (it can invoke a stop method in your running application). It also has a little bit of extra functionality like optionally redirecting your stdout and stderr to separate log files, and running the service wrapper exe directly (assuming you renamed it to your service name) runs a non-service mode console that lets you see the output immediately. You probably want to use procrun's "jvm mode". Read the details of how to do that in the documentation.

If you need additional fancier capabilities, such as if the service needs to be able to restart itself, take a look at YAJSW (Yet Another Java Service Wrapper) instead. YAJSW is quite likely better overall, but I haven't tried it yet.

PolyTekPatrick
  • 3,122
  • 1
  • 25
  • 19