Module: Loops::Daemonize
- Defined in:
- lib/loops/daemonize.rb
Class Method Summary collapse
- .check_pid(pid_file) ⇒ Object
- .create_pid(pid_file) ⇒ Object
- .daemonize(app_name) ⇒ Object
- .read_pid(pid_file) ⇒ Object
Class Method Details
.check_pid(pid_file) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/loops/daemonize.rb', line 11 def self.check_pid(pid_file) pid = read_pid(pid_file) return false if pid.zero? if defined?(::JRuby) system "kill -0 #{pid} &> /dev/null" return $? == 0 else Process.kill(0, pid) end true rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM false end |
.create_pid(pid_file) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/loops/daemonize.rb', line 25 def self.create_pid(pid_file) if File.exist?(pid_file) puts "Pid file #{pid_file} exists! Checking the process..." if check_pid(pid_file) puts "Can't create new pid file because another process is runnig!" return false end puts "Stale pid file! Removing..." File.delete(pid_file) end File.open(pid_file, 'w') do |f| f.puts(Process.pid) end return true end |
.daemonize(app_name) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/loops/daemonize.rb', line 43 def self.daemonize(app_name) if defined?(::JRuby) puts "WARNING: daemonize method is not implemented for JRuby (yet), please consider using nohup." return end fork && exit # Fork and exit from the parent # Detach from the controlling terminal unless sess_id = Process.setsid raise Daemons.RuntimeException.new('cannot detach from controlling terminal') end # Prevent the possibility of acquiring a controlling terminal trap 'SIGHUP', 'IGNORE' exit if pid = fork $0 = app_name if app_name Dir.chdir(Loops.root) # Make sure we're in the working directory File.umask(0000) # Insure sensible umask return sess_id end |
.read_pid(pid_file) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/loops/daemonize.rb', line 3 def self.read_pid(pid_file) File.open(pid_file) do |f| f.gets.to_i end rescue Errno::ENOENT 0 end |