A common question people ask about JRuby is "why doesn't <arbitrary
daemonization gem> work on JRuby?"
Usually the answer is that we don't support "fork". Most of the
daemonization libraries for MRI fork the process and then terminate,
leaving the forked child to run headless. It's simply not possible to
support fork on JRuby, since the JVM spins up lots of threads and
signal handlers that don't migrate to the child process. So what can
we do?
Well if you are able to have the child process start up anew, you may
be able to use my Spoon gem or the posix_spawn C call it leverages.
Here's an example of launching JRuby in daemon mode using Spoon:
{code}
require 'rubygems'
require 'spoon'
Spoon.spawnp 'jruby', *ARGV
{code}
Of course this is a trivial example, which doesn't even attempt to
pass along -J flags to JRuby or Ruby flags like -v and -w, but you get
the idea. What happens here is that Spoon.spawnp uses posix_spawnp to
launch "jruby" from PATH with the specified arguments. posix_spawn and
posix_spawnp are not standard POSIX functions, but they are supported
by basically all the major Unix variants:
http://www.opengroup.org/onlinepubs/009695399/functions/posix_spawn.html
Spoon also includes an example (directly in lib/spoon.rb) that shows
the potential:
pid = Spoon.spawn('/usr/bin/vim')
Process.waitpid(pid)
This launches vim as a child process, but avoids all the terminal
breakage that normally comes from JRuby launching a tty-sensitive
subprocess. The sub-vim works perfectly.
posix_spawn is what Wayne Meissner describes as "fork/exec on
steroids". It provides just about every combination of security, IO,
and subprocess wrangling you might expect to do in a typical fork/exec
scenario without actually using fork/exec. It also does it in one
shot, avoiding issues like those I mention in my blog post about
fork/exec on JRuby:
http://blog.headius.com/2009/05/fork-and-exec-on-jvm-jruby-to-rescue.html
Anyway, have fun with Spoon and posix_spawn :)
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
on 2010-03-07 15:57
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.