Module: Kernel

Defined in:
lib/epitools/its.rb,
lib/epitools/daemonize.rb

Instance Method Summary collapse

Instance Method Details

#daemonize(chdir = nil, &on_sighup) ⇒ Object

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epitools/daemonize.rb', line 5

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

#safe_fork(delay = 5) ⇒ Object

Try to fork if at all possible retrying every delay sec (5s default) if the maximum process limit for the system has been reached



32
33
34
35
36
37
# File 'lib/epitools/daemonize.rb', line 32

def safe_fork(delay=5)
  fork
rescue Errno::EWOULDBLOCK
  sleep(delay)
  retry
end