Module: Daemontor

Defined in:
lib/daemontor.rb,
lib/daemontor/version.rb

Overview

This module’s 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 =
"1.0.0"

Instance Method Summary collapse

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 afterward pass a boolean true value and you will get access to the process once the child is detached.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/daemontor.rb', line 18

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_intObject

********************************************************************************* You might want to overide the following methods to do something a little smarter. *********************************************************************************



47
48
49
50
# File 'lib/daemontor.rb', line 47

def p_int
  warn "Process interupt recieved.  Killing process"
  p_kill
end

#p_killObject



62
63
64
65
# File 'lib/daemontor.rb', line 62

def p_kill
  warn "Killing process #{Process.pid.to_s}"
  exit
end

#p_quitObject



52
53
54
55
# File 'lib/daemontor.rb', line 52

def p_quit
  warn "Process quit recieved.  Killing process"
  p_kill
end

#p_termObject



57
58
59
60
# File 'lib/daemontor.rb', line 57

def p_term
  warn "Process termination recieved.  Killing process"
  p_kill
end