2012-09-25 12:34:10 -04:00
|
|
|
module Process
|
2013-11-20 18:05:51 -05:00
|
|
|
|
|
|
|
# This overrides the default version because it is broken if it
|
|
|
|
# exists.
|
|
|
|
|
2012-09-25 12:34:10 -04:00
|
|
|
def self.daemon(nochdir=false, noclose=false)
|
|
|
|
exit if fork # Parent exits, child continues.
|
|
|
|
|
|
|
|
Process.setsid # Become session leader.
|
|
|
|
|
|
|
|
exit if fork # Zap session leader. See [1].
|
|
|
|
|
|
|
|
Dir.chdir "/" unless nochdir # Release old working directory.
|
|
|
|
|
|
|
|
if !noclose
|
2013-12-05 12:51:39 -05:00
|
|
|
STDIN.reopen File.open("/dev/null", "r")
|
|
|
|
|
|
|
|
null_out = File.open "/dev/null", "w"
|
|
|
|
STDOUT.reopen null_out
|
|
|
|
STDERR.reopen null_out
|
2012-09-25 12:34:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
0
|
2013-11-20 18:05:51 -05:00
|
|
|
end
|
2012-09-25 12:34:10 -04:00
|
|
|
end
|