1 ################################################################################
5 ################################################################################
6 # methods for becoming a daemon on Unix-like operating systems
9 ################################################################################
10 def platform_daemon (&block)
13 trap("TERM") { exit(0) }
14 sess_id = Process.setsid
15 STDIN.reopen("/dev/null")
16 STDOUT.reopen("#{RAILS_ROOT}/log/ferret_server.out", "a")
22 ################################################################################
23 # stop the daemon, nicely at first, and then forcefully if necessary
26 raise "ferret_server doesn't appear to be running" unless pid
27 $stdout.puts("stopping ferret server...")
28 Process.kill("TERM", pid)
29 30.times { Process.kill(0, pid); sleep(0.5) }
30 $stdout.puts("using kill -9 #{pid}")
32 rescue Errno::ESRCH => e
33 $stdout.puts("process #{pid} has stopped")
35 File.unlink(@cfg.pid_file) if File.exist?(@cfg.pid_file)
38 ################################################################################
42 rescue Errno::EWOULDBLOCK
43 raise if @fork_tries >= 20
49 #################################################################################
50 # create the PID file and install an at_exit handler
52 raise "ferret_server may already be running, a pid file exists: #{@cfg.pid_file}" if read_pid_file
53 open(@cfg.pid_file, "w") {|f| f << Process.pid << "\n"}
54 at_exit { File.unlink(@cfg.pid_file) if read_pid_file == Process.pid }
57 #################################################################################
59 File.read(@cfg.pid_file).to_i if File.exist?(@cfg.pid_file)