hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Daemonize #

by why in inspect

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.

said on 02 Feb 2005 at 12:44

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.

said on 02 Feb 2005 at 13:58

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. :)

said on 02 Feb 2005 at 14:35

Daniel,

I’m interested in your package. So where’s the plug?

said on 02 Feb 2005 at 14:41

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.

said on 02 Feb 2005 at 15:00

Oh, also note that Process#daemon was checked into HEAD . See ruby-dev:24106 and ruby-talk:111139.

said on 02 Feb 2005 at 15:30

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!”

said on 02 Feb 2005 at 15:41

Funny, it doesn’t seem to be working on my Windows box. :-P

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!)

said on 02 Feb 2005 at 16:06

TOGoS, yes, I’m well aware of that, hence the winkey smiley. :)

said on 03 Feb 2005 at 01:08

isnt’ a buzz already a noise ?

said on 03 Feb 2005 at 03:04

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?

said on 03 Feb 2005 at 09:40

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. :)

said on 03 Feb 2005 at 11:29

I like ‘fork and exit’ over ‘exit if fork’. Daemonize uses the latter :(

said on 04 Feb 2005 at 13:19

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.