Daemonize #
Here’s one from an RAA dig a few weeks ago. Wanting to turn some of my Ruby jobs into daemons, I went looking for anything slightly formal. And Daemonize is 59 lines of Ruby that fits just right.
require 'daemonize' include Daemonize daemonize() loop do # Your code cycles here end
Daemonize
will safely fork, retrying if the maximum process limit has been reached. It detaches from the controlling terminal, resets the cwd and umask, then closes all file descriptors. And so my little scripts buzz around noiselessly.
Lucas Carlson
I love how simple forking is with Ruby. I use fork in my Rails apps as described at http://tech.rufy.com/ to do regular maintenance.
Daniel Berger
Funny, it doesn’t seem to be working on my Windows box. :-P
Let me shamelessly plug my own package – win32-service – to accomplish the equivalent of daemonization on Win32, for those who are curious. :)
Ed
Daniel,
I’m interested in your package. So where’s the plug?
Daniel Berger
Ed, you’ll have to download the package (it’s on the RAA ) and read the docs. You can take a look here for an example of setting up the Gem Server as a Windows service.
It’s not nearly as easy as just calling “daemonize”, but you have greater control over it once it’s started, plus some automatic logging to the Event Log.
Daniel Berger
Oh, also note that Process#daemon was checked into HEAD . See ruby-dev:24106 and ruby-talk:111139.
why
I want a banner ad for win32-utils for my site. It should be flashing red and yellow with an astronaut saying, “I can’t believe I left my SAPI hooks at home.”
Beat.
“With the wife and kids!”
TOGoS
I think that’s because Windows is missing fork(). Rather makes this kind of thing a pain in the butt (no open(”-|”), for instance – you have to use system(). I’m not even sure that you can use pipes!)
Daniel Berger
TOGoS, yes, I’m well aware of that, hence the winkey smiley. :)
kaspar
isnt’ a buzz already a noise ?
Phil
Daniel,
Would it be possible to create a ‘fork’ method in your win32-service module (actually, put in into Kernel) so that if you’re running on Windows you can use ‘fork’ just as you would on *nix?
Seems like your win32 packages should be standard in the ‘one-click’ Windows install version and ‘fork’ should just work seamlessly without needing to do any special requires. I think it’s possible, what do you think?
Daniel Berger
Phil,
Actually, we do already have a fork method for Windows in the win32-process package. But, it doesn’t behave quite the same way as it does in Unix. Generally speaking, use threads instead of fork if you want to keep your code portable.
You could write your own Process.fork by tailoring your own version of Process.create (again, in win32-process), which is a wrapper for the Windows CreateProcess() function.
As for including our packages in the one-click version, the topic has come up before. You’ll have to bug Curt Hibbs about it. Leave a friendly note on the RubyForge project page for the one-click installer. :)
drbrain
I like ‘fork and exit’ over ‘exit if fork’. Daemonize uses the latter :(
Travis Whitton
I just released a new version (0.1.1). Drbrain, exit if fork has been changed to fork and exit. A few other enhancements have also been made. Thank you all for the interest in my library.
http://grub.ath.cx/daemonize/
Comments are closed for this entry.