56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/forker.rb', line 56
def self.fork!(opts={})
opts = { :log => "/dev/null", :pid => "/var/run/#{File.basename($0)}.pid" }.merge(opts)
$stdout.sync = $stderr.sync = true
$stdin.reopen("/dev/null")
exit if fork
Process.setsid
exit if fork
Dir.chdir("/") if opts[:chdir]
File.umask(0000) if opts[:umask]
if File.exist?(opts[:pid])
begin
existing_pid = File.read(opts[:pid]).to_i
Process.kill(0, existing_pid) abort "error: existing process #{existing_pid} using this pidfile, exiting"
rescue Errno::ESRCH
puts "warning: removing stale pidfile with pid #{existing_pid}"
end
end
File.open(opts[:pid], 'w') { |f| f.write($$) }
at_exit do
( File.read(opts[:pid]).to_i == $$ and File.unlink(opts[:pid]) ) rescue nil
end
puts "forked process is #{$$}"
puts "output redirected to #{opts[:log]}"
$stdout.reopen(opts[:log], 'a')
$stderr.reopen(opts[:log], 'a')
$stdout.sync = $stderr.sync = true
end
|