Run your jruby application as a unix daemon, via jsvc. Works around the fact that you can't really
fork from jruby/java.
With jruby-jsvc you can check your application has started properly before it is backgrounded, and then control it like you would other unix daemons. If you've got commons-daemon-1.0.6 you can make use of SIGUSR2 to have some custom signal handling in your daemon (roll your logs, reload your configuration).
- Install jsvc-jruby. Check out from source, then run mvn package. For development purposes, you probably just want to copy
jruby-jsvc.jarin to you project directory. I'll leave it to you to decide how you want to distribute to your server (Debian packaging should be coming soon).
- Install jsvc. Your system may come with it, or you may have to build it yourself from http://commons.apache.org/daemon/. It isn't the most difficult thing to get running, but jruby-jsvc expects the
jsvcexcecutable to be on your path, and expects the commons-daemon jar to be installed in to
- Take a look at
example/lib/crazy_daemon.rb- you need to create a Daemon singleton module underneath your application's namespace, something like
Crazy::Daemon. This should have a
setup?should return true, and is basically a detection method to check that the daemon was defined and was able to bring up whatever resources it needed to be daemonic.
startis called once the daemon has been backgrounded and is your signal to start accepting connections or chomping strings or whatever your application does.
- Create a boot-up script. This should require your daemon module and initialize your application so that it is ready to start serving once Daemon.start is called. There are a couple of examples in example/bin - one which succeeds, the other fails using the DaemonInitException.
Create an init.d script that invokes
jsvc-wrapper.bin(or a copy of it). You'll need to set a few environment variables, and possibly tweak
jsvc-wrapper.sha bit. For development, you can call
jsvc-wrapper.shdirectly to test running your app as a daemon - Just invoke it from your applications root directory, with something like (works for the example application):
JRUBY_JSVC_JAR=../target/jsvc-[SOME-VERSION].jar JRUBY_DAEMON_DEV=true MODULE_NAME=Crazy SCRIPT_NAME=good_daemon ../bin/jsvc-wrapper.sh start