Sha256: b793315fd69bd2a70358ca2400b66da616e59c44772c7796cddb7e2957b9e2c7

Contents?: true

Size: 1.3 KB

Versions: 16

Compression:

Stored size: 1.3 KB

Contents

# This is largely based on the Daemonize library by Travis Whitton and
# Judson Lester. http://grub.ath.cx/daemonize. I cleaned it up a bit to
# meet Adhearsion's quality standards.
module Adhearsion
  module CustomDaemonizer
  
    # Try to fork if at all possible retrying every 5 sec if the
    # maximum process limit for the system has been reached
    def safefork
      begin
        pid = fork
        return pid if pid
      rescue Errno::EWOULDBLOCK
        sleep 5
        retry
      end
    end

    # This method causes the current running process to become a daemon
    def daemonize(log_file='/dev/null')
      oldmode = 0
      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 # Ensure sensible umask

      STDIN.reopen "/dev/null"
      STDOUT.reopen '/dev/null', "a"
      STDERR.reopen log_file 
      return oldmode ? sess_id : 0
    end
  end
end

Version data entries

16 entries across 16 versions & 7 rubygems

Version Path
eric-adhearsion-0.7.999 lib/adhearsion/core_extensions/custom_daemonizer.rb
eric-adhearsion-0.8.0 lib/adhearsion/foundation/custom_daemonizer.rb
jicksta-adhearsion-0.7.999 lib/adhearsion/foundation/custom_daemonizer.rb
jicksta-adhearsion-0.8.0 lib/adhearsion/foundation/custom_daemonizer.rb
jicksta-adhearsion-0.8.2 lib/adhearsion/foundation/custom_daemonizer.rb
jicksta-adhearsion-0.8.3 lib/adhearsion/foundation/custom_daemonizer.rb
kenwiesner-adhearsioncw-0.8.3 lib/adhearsion/foundation/custom_daemonizer.rb
mtrudel-adhearsion-0.8.3 lib/adhearsion/foundation/custom_daemonizer.rb
sant0sk1-adhearsion-0.7.999 lib/adhearsion/core_extensions/custom_daemonizer.rb
sevenscale-adhearsion-0.7.1000 lib/adhearsion/core_extensions/custom_daemonizer.rb
sevenscale-adhearsion-0.7.1003 lib/adhearsion/core_extensions/custom_daemonizer.rb
sevenscale-adhearsion-0.8.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.0 lib/adhearsion/foundation/custom_daemonizer.rb