Sha256: 5dfdb383df134e71f23d572bf53ac637360a8162db6a50d84821c6633b642e42

Contents?: true

Size: 1.2 KB

Versions: 27

Compression:

Stored size: 1.2 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

27 entries across 27 versions & 1 rubygems

Version Path
epitools-0.5.136 lib/epitools/daemonize.rb
epitools-0.5.134 lib/epitools/daemonize.rb
epitools-0.5.133 lib/epitools/daemonize.rb
epitools-0.5.131 lib/epitools/daemonize.rb
epitools-0.5.130 lib/epitools/daemonize.rb
epitools-0.5.129 lib/epitools/daemonize.rb
epitools-0.5.128 lib/epitools/daemonize.rb
epitools-0.5.126 lib/epitools/daemonize.rb
epitools-0.5.125 lib/epitools/daemonize.rb
epitools-0.5.124 lib/epitools/daemonize.rb
epitools-0.5.123 lib/epitools/daemonize.rb
epitools-0.5.122 lib/epitools/daemonize.rb
epitools-0.5.121 lib/epitools/daemonize.rb
epitools-0.5.119 lib/epitools/daemonize.rb
epitools-0.5.118 lib/epitools/daemonize.rb
epitools-0.5.116 lib/epitools/daemonize.rb
epitools-0.5.115 lib/epitools/daemonize.rb
epitools-0.5.114 lib/epitools/daemonize.rb
epitools-0.5.113 lib/epitools/daemonize.rb
epitools-0.5.112 lib/epitools/daemonize.rb