X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=lib%2Fmcollective%2Funix_daemon.rb;fp=lib%2Fmcollective%2Funix_daemon.rb;h=dfacdfb437b9c5019166da110a146f5dc1f2cbe2;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/unix_daemon.rb b/lib/mcollective/unix_daemon.rb new file mode 100644 index 0000000..dfacdfb --- /dev/null +++ b/lib/mcollective/unix_daemon.rb @@ -0,0 +1,37 @@ +module MCollective + class UnixDaemon + # Daemonize the current process + def self.daemonize + fork do + Process.setsid + exit if fork + Dir.chdir('/tmp') + STDIN.reopen('/dev/null') + STDOUT.reopen('/dev/null', 'a') + STDERR.reopen('/dev/null', 'a') + + yield + end + end + + def self.daemonize_runner(pid=nil) + raise "The Unix Daemonizer can not be used on the Windows Platform" if Util.windows? + + UnixDaemon.daemonize do + if pid + begin + File.open(pid, 'w') {|f| f.write(Process.pid) } + rescue Exception => e + end + end + + begin + runner = Runner.new(nil) + runner.run + ensure + File.unlink(pid) if pid && File.exist?(pid) + end + end + end + end +end