Sha256: 9b77365cdb0681290af7bc2696b1639b48cf13f7ed61d2cfbcc0e9f7378aeb76

Contents?: true

Size: 1.3 KB

Versions: 19

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, "a"
      return oldmode ? sess_id : 0
    end
  end
end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
adhearsion-1.2.6 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.2.5 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.2.4 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.2.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.2.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.2.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.1.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.1.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.0.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.0.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-cw-1.0.2.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-cw-1.0.2.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-cw-1.0.2.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.0.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-1.0.0 lib/adhearsion/foundation/custom_daemonizer.rb
rene-adhearsion-0.8.6 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.6 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.5 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-0.8.4 lib/adhearsion/foundation/custom_daemonizer.rb