Module: Daemon

Included in:
Server
Defined in:
lib/arldap/daemon.rb

Instance Method Summary collapse

Instance Method Details

#daemonize(logger = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/arldap/daemon.rb', line 8

def daemonize(logger = nil)
  # This causes the grandchild process to be orphaned, 
  # so the init process is responsible for cleaning it up.
  Kernel.fork and Kernel.exit
  Process.setsid
  Kernel.fork and Kernel.exit

  File.umask 0
  Dir.chdir '/'

  ObjectSpace.each_object(IO) do |io|
    unless (logger and logger.logdev.dev == io)
      io.close rescue nil
    end
  end

  STDIN.reopen( '/dev/null')
  STDOUT.reopen('/dev/null', 'a')
  STDERR.reopen('/dev/null', 'a')
end