Method: Mongrel::Configurator#daemonize

Defined in:
lib/mongrel/configurator.rb

#daemonize(options = {}) ⇒ Object

Daemonizes the current Ruby script turning all the listeners into an actual “server” or detached process. You must call this before frameworks that open files as otherwise the files will be closed by this function.

Does not work for Win32 systems (the call is silently ignored).

Requires the following options or defaults:

  • :cwd => Directory to change to.

  • :log_file => Where to write STDOUT and STDERR.

It is safe to call this on win32 as it will only require the daemons gem/library if NOT win32.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mongrel/configurator.rb', line 185

def daemonize(options={})
  ops = resolve_defaults(options)
  # save this for later since daemonize will hose it
  if RUBY_PLATFORM !~ /mswin/
    require 'daemons/daemonize'

    logfile = ops[:log_file]
    if logfile[0].chr != "/"
      logfile = File.join(ops[:cwd],logfile)
      if not File.exist?(File.dirname(logfile))
        log "!!! Log file directory not found at full path #{File.dirname(logfile)}.  Update your configuration to use a full path."
        exit 1
      end
    end

    Daemonize.daemonize(logfile)

    # change back to the original starting directory
    Dir.chdir(ops[:cwd])

  else
    log "WARNING: Win32 does not support daemon mode."
  end
end