Module: Process

Defined in:
lib/environment.rb

Class Method Summary collapse

Class Method Details

.daemon(nochdir = nil, noclose = nil) ⇒ Object

Supply daemon for pre ruby 1.9

Adapted from lib/active_support/core_ext/process/daemon.rb


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/environment.rb', line 40

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

    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