tags:

views:

86

answers:

3

I want to write a Ruby program that will always be running in the background (a daemon) on my mac. Can someone point me in the right direction on how this would be done? Thanks.

+1  A: 

This is a module to daemonize your code. Here's an offshoot that wraps an existing script.

Essentially it boils down to this (from Travis Whitton's Daemonize.rb, the first link above, modified for some program I wrote ages ago):

private
# This method causes the current running process to become a daemon
# If closefd is true, all existing file descriptors are closed
def daemonize(pathStdErr, oldmode=0, closefd=false)
    srand # Split rand streams between spawning and daemonized process
    safefork and exit# Fork and exit from the parent

    # Detach from the controlling terminal
    unless sess_id = Process.setsid
        raise 'Cannot detach from controlled terminal'
    end

    # Prevent the possibility of acquiring a controlling terminal
    if oldmode.zero?
        trap 'SIGHUP', 'IGNORE'
        exit if pid = safefork
    end

    Dir.chdir "/"   # Release old working directory
    File.umask 0000 # Insure sensible umask

    if closefd
        # Make sure all file descriptors are closed
        ObjectSpace.each_object(IO) do |io|
            unless [STDIN, STDOUT, STDERR].include?(io)
                io.close rescue nil
            end
        end
    end

    STDIN.reopen "/dev/null"       # Free file descriptors and
    STDOUT.reopen "/dev/null"   # point them somewhere sensible
    STDERR.reopen pathStdErr, "w"           # STDOUT/STDERR should go to a logfile
    return oldmode ? sess_id : 0   # Return value is mostly irrelevant
end

# Try to fork if at all possible retrying every 5 sec if the
# maximum process limit for the system has been reached
def safefork
    tryagain = true
    while tryagain
        tryagain = false
        begin
            if pid = fork
                return pid
            end
        rescue Errno::EWOULDBLOCK
            sleep 5
            tryagain = true
        end
    end
end
Mark
Mark, while this is the right code for more vanilla Unix systems, and it will more-or-less work on Mac OS X, it's really not the right recipe... I don't have a canned recipe handy for registering a ruby script with launchd, but that's what the OP probably should be looking for. :-)
Kaelin Colclasure
@Kaelin, excellent point. I should have read the question more carefully. Unfortunately, I don't no nothing about no Macs...
Mark
+2  A: 

Ah, Google to the rescue! Check out

http://fitzgeraldsteele.wordpress.com/2009/05/04/launchd-example-start-web-server-at-boot-time/

wherein a helpful blogger provides an example of writing a launchd plist to launch a ruby Web application server.

Kaelin Colclasure
Glad you thought it was helpful! I've personally come to like launchd...for one it can restart your process if it dies unexpectedly.
fitzgeraldsteele
+3  A: 

Use Daemonize.rb

require 'daemons'
Daemons.daemonize

Very simple Sample: http://github.com/utkarsh2012/backitup/blob/master/backitup.rb

How to install daemons gem:

gem install daemons
zengr
This might be a stupid question, but where is the daemonize.rb file? Is it a gem, is there a place on the web where I can find it, is it the standard library, or what?
agentbanks217
It's a gem. You just install and start using it.
zengr
Ok thanks a lot.
agentbanks217