Module: Daemontor
- Defined in:
- lib/daemontor.rb
Overview
This modules purpose is to daemonize processes so they can be run in the background like a standard UNIX daemon. When Daemontor::daemonize is called the parent process fork()s a new child process and calls Process.detach so we don’t have to wait for the child process to exit. The parent process then exits and the child process goes happily on. Since we are calling Process.detach we do not need to do a double-fork
Thanks to the PickAxe book for good process handling information as well as hints from Travis Whitton’s Daemonize module: grub.ath.cx/daemonize/
Constant Summary collapse
- VERSION =
'0.0.1'
Instance Method Summary collapse
-
#daemonize!(keep_parent_alive = false) ⇒ Object
This is the function that makes it all happen.
-
#p_int ⇒ Object
********************************************************************************* You might want to overide the following methods to do something a little smarter.
- #p_kill ⇒ Object
- #p_quit ⇒ Object
- #p_term ⇒ Object
Instance Method Details
#daemonize!(keep_parent_alive = false) ⇒ Object
This is the function that makes it all happen. By default the parent process will be killed off. If you want to do something with the parent afterword pass a boolean true value and you will get access to the process back once the child is detached.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/daemontor.rb', line 40 def daemonize!(keep_parent_alive = false) puts "Doing fork " + Process.pid.to_s if $DEBUG if((cpid = fork).nil?) # Only the child processes should get here trap("INT", proc {p_int} ) trap("QUIT", proc {p_quit} ) trap("TERM", proc {p_term} ) trap("KILL", proc {p_kill} ) unless $DEBUG STDIN.reopen "/dev/null" STDOUT.reopen "/dev/null", "a" STDERR.reopen STDOUT end else # Only the parent processes should get here. We detach from a specific # PID so we don't globally detach from all PIDs. This may be handy when you need some # children to return and some not to. The logic isn't here to do that yet, but it # should be relatively easy to implement with this already in place. puts "Detaching from child process #{cpid}" if $DEBUG Process.detach(cpid) exit unless keep_parent_alive end return cpid end |
#p_int ⇒ Object
********************************************************************************* You might want to overide the following methods to do something a little smarter. *********************************************************************************
72 73 74 75 |
# File 'lib/daemontor.rb', line 72 def p_int warn "Process interupt recieved. Killing process" p_kill end |
#p_kill ⇒ Object
87 88 89 90 |
# File 'lib/daemontor.rb', line 87 def p_kill warn "Killing process #{Process.pid.to_s}" exit end |
#p_quit ⇒ Object
77 78 79 80 |
# File 'lib/daemontor.rb', line 77 def p_quit warn "Process quit recieved. Killing process" p_kill end |
#p_term ⇒ Object
82 83 84 85 |
# File 'lib/daemontor.rb', line 82 def p_term warn "Process termination recieved. Killing process" p_kill end |