T
Tim Pease
module Process
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.
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
return 0
end
end
This is the standard code to daemonize a process. The use of "exit"
bothers me slightly, as it will call all "at_exit" handlers in the
parent. I do not believe this is the desired behavior; the "at_exit"
handlers will free resources that the daemonized process might need.
Instead, "exit!" should be used to prevent "at_exit" handlers from
being called.
Any thoughts about this subtle change? Are there any undesirable side
effects that arise from using exit! instead of exit ?
Blessings,
TwP
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.
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
return 0
end
end
This is the standard code to daemonize a process. The use of "exit"
bothers me slightly, as it will call all "at_exit" handlers in the
parent. I do not believe this is the desired behavior; the "at_exit"
handlers will free resources that the daemonized process might need.
Instead, "exit!" should be used to prevent "at_exit" handlers from
being called.
Any thoughts about this subtle change? Are there any undesirable side
effects that arise from using exit! instead of exit ?
Blessings,
TwP