Module: Kernel

Defined in:
lib/thin/daemonizing.rb

Instance Method Summary collapse

Instance Method Details

#daemonizeObject

Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal. Taken from ActiveSupport.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/thin/daemonizing.rb', line 7

def daemonize
  exit if fork                   # Parent exits, child continues.
  Process.setsid                 # Become session leader.
  exit if fork                   # Zap session leader. See [1].
  Dir.chdir "/"                  # Release old working directory.
  File.umask 0000                # Ensure sensible umask. Adjust as needed.
  STDIN.reopen "/dev/null"       # Free file descriptors and
  STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
  STDERR.reopen STDOUT           # STDOUT/ERR should better go to a logfile.
  trap("TERM") { exit }
end