Sha256: ed17e531cdf00226839907eb7ee4b7e0c7b1ac984fa994d597958a15c3a38788

Contents?: true

Size: 1.21 KB

Versions: 22

Compression:

Stored size: 1.21 KB

Contents

module Kernel
  # This method causes the current running process to become a daemon
  # All further printing is relied to the error.log
  # FIXME doesn't belong into Butler::Bot, rather into botcontrol
  def daemonize(chdir=nil, &on_sighup)
    srand # Split rand streams between spawning and daemonized process
    safe_fork and exit # Fork and exit from the parent

    # Detach from the controlling terminal
    raise "Can't detach from controlling terminal" unless sess_id = Process.setsid

    # Prevent the possibility of acquiring a controlling terminal
    trap('SIGHUP', 'IGNORE')
    exit if safe_fork

    # In daemon mode, a SIGHUP means termination
    trap('SIGHUP', &on_sighup)

    # We can't get the originally controlling terminals stdout/stdin anymore
    STDIN.reopen("/dev/null")
    STDOUT.reopen("/dev/null", "a")
    STDERR.reopen(STDOUT)
    
    Dir.chdir(chdir) if chdir
    File.umask 0033 # FIXME ask somebody knowledgable about a sensible value

    sess_id
  end

  # Try to fork if at all possible retrying every +delay+ sec (5s default)
  # if the maximum process limit for the system has been reached
  def safe_fork(delay=5)
    fork
  rescue Errno::EWOULDBLOCK
    sleep(delay)
    retry
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
epitools-0.5.103 lib/epitools/daemonize.rb
epitools-0.5.100 lib/epitools/daemonize.rb
epitools-0.5.99 lib/epitools/daemonize.rb
epitools-0.5.98 lib/epitools/daemonize.rb
epitools-0.5.97 lib/epitools/daemonize.rb
epitools-0.5.96 lib/epitools/daemonize.rb
epitools-0.5.95 lib/epitools/daemonize.rb
epitools-0.5.94 lib/epitools/daemonize.rb
epitools-0.5.93 lib/epitools/daemonize.rb
epitools-0.5.92 lib/epitools/daemonize.rb
epitools-0.5.91 lib/epitools/daemonize.rb
epitools-0.5.90 lib/epitools/daemonize.rb
epitools-0.5.89 lib/epitools/daemonize.rb
epitools-0.5.88 lib/epitools/daemonize.rb
epitools-0.5.87 lib/epitools/daemonize.rb
epitools-0.5.86 lib/epitools/daemonize.rb
epitools-0.5.85 lib/epitools/daemonize.rb
epitools-0.5.84 lib/epitools/daemonize.rb
epitools-0.5.83 lib/epitools/daemonize.rb
epitools-0.5.82 lib/epitools/daemonize.rb