Sha256: 8a35c6f0a0b77bdef3aefec8b3be1b3f79166934e7efdb9409848463f7c8fdfc

Contents?: true

Size: 795 Bytes

Versions: 10

Compression:

Stored size: 795 Bytes

Contents

module Process

  # Turns the current script into a daemon process
  # that detaches from the console. It can be shut
  # down with a TERM signal.
  #
  # CREDIT: David Heinemeier Hansson

  def self.daemon(nochdir=nil, noclose=nil)
    exit if fork # Parent exits, child continues.
    Process.setsid # Become session leader.
    exit if fork # Zap session leader. See [1].

    unless nochdir
      Dir.chdir "/" # Release old working directory.
    end

    File.umask 0000 # Ensure sensible umask. Adjust as needed.

    unless noclose
      STDIN.reopen "/dev/null" # Free file descriptors and
      STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
      STDERR.reopen '/dev/null', 'a'
    end

    trap("TERM") { exit }

    return 0
  end unless respond_to?(:daemon)

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/process/daemon.rb
facets-3.1.0 lib/core/facets/process/daemon.rb
facets-3.0.0 lib/core/facets/process/daemon.rb
facets-2.9.3 lib/core/facets/process/daemon.rb
facets-2.9.2 src/core/facets/process/daemon.rb
facets-2.9.2 lib/core/facets/process/daemon.rb
facets-2.9.1 lib/core/facets/process/daemon.rb
facets-2.9.0 lib/core/facets/process/daemon.rb
facets-2.9.0.pre.2 lib/core/facets/process/daemon.rb
facets-2.9.0.pre.1 lib/core/facets/process/daemon.rb