Permalink
Browse files

fix for double fork

  • Loading branch information...
1 parent c291046 commit 2fce0afe682b6bb52ed83586c64126eef9797339 @gnufied committed May 20, 2009
Showing with 20 additions and 10 deletions.
  1. +20 −10 lib/backgroundrb/bdrb_start_stop.rb
View
30 lib/backgroundrb/bdrb_start_stop.rb
@@ -36,14 +36,14 @@ def stop
# 3 program is not running
def status
@status ||= begin
- if pidfile_exists? and process_running?
- 0
- elsif pidfile_exists? # but not process_running
- 1
- else
- 3
- end
- end
+ if pidfile_exists? and process_running?
+ 0
+ elsif pidfile_exists? # but not process_running
+ 1
+ else
+ 3
+ end
+ end
return @status
end
@@ -91,15 +91,25 @@ def start_bdrb
require "bdrb_job_queue"
require "backgroundrb_server"
- main_pid = fork do
+ #proper way to daemonize - double fork to ensure parent can have no interest in the grandchild
+ if fork # Parent exits, child continues.
+ sleep(5)
+ exit(0)
+ else
+ Process.setsid # Become session leader.
+
+ op = File.open(PID_FILE, "w")
+ op.write(Process.pid().to_s)
+ op.close
+
if BDRB_CONFIG[:backgroundrb][:log].nil? or BDRB_CONFIG[:backgroundrb][:log] != 'foreground'
redirect_io(SERVER_LOGGER)
end
$0 = "backgroundrb master"
BackgrounDRb::MasterProxy.new()
end
- File.open(PID_FILE, "w") {|pidfile| pidfile.write(main_pid)}
+ #File.open(PID_FILE, "w") {|pidfile| pidfile.write(main_pid)}
end
def kill_process(pid_file)

0 comments on commit 2fce0af

Please sign in to comment.