Make a Ruby program a daemon?

后端 未结 5 762
旧巷少年郎
旧巷少年郎 2020-12-01 10:53

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 d

相关标签:
5条回答
  • 2020-12-01 11:13

    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
    
    0 讨论(0)
  • 2020-12-01 11:16

    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.

    0 讨论(0)
  • 2020-12-01 11:26

    Ruby 1.9.x has now the following:

    Process.daemon
    

    Put it in your code and that's it.

    Taken from "Daemon Processes in Ruby."

    0 讨论(0)
  • 2020-12-01 11:29

    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
    
    0 讨论(0)
  • 2020-12-01 11:32

    Need to see the daemons-rails gem for Rails 3 (based on rails_generator):

    https://github.com/mirasrael/daemons-rails

    Possible to generate daemon stub like this:

    rails generate daemon <name>
    

    Features:

    • individual control script per daemon
    • rake:daemon command per daemon
    • capistrano friendly
    • app-wide control script
    • monitoring API
    • possible multiple daemon sets
    0 讨论(0)
提交回复
热议问题