Module: Daemonize

Defined in:
lib/daemons/daemonize.rb

Class Method Summary collapse

Class Method Details

.call_as_daemon(block, logfile_name = nil, app_name = nil) ⇒ Object

Call a given block as a daemon



39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/daemons/daemonize.rb', line 39

def call_as_daemon(block, logfile_name = nil, app_name = nil)
  # we use a pipe to return the PID of the daemon
  rd, wr = IO.pipe
  
  if tmppid = safefork
    # in the parent
    
    wr.close
    pid = rd.read.to_i
    rd.close
    
    Process.waitpid(tmppid)
    
    return pid
  else
    # in the child
    
    rd.close
    
    # 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 = safefork

    wr.write Process.pid
    wr.close
    
    $0 = app_name if app_name
    
    # Release old working directory
    Dir.chdir "/"   

    close_io()

    redirect_io(logfile_name)  
  
    block.call
    
    exit
  end
end

.close_ioObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/daemons/daemonize.rb', line 118

def close_io()
  # Make sure all input/output streams are closed
  # Part I: close all IO objects (except for STDIN/STDOUT/STDERR)
  ObjectSpace.each_object(IO) do |io|
    unless [STDIN, STDOUT, STDERR].include?(io)
      begin
        unless io.closed?
          io.close
        end
      rescue ::Exception
      end
    end
  end
  
  # Make sure all input/output streams are closed
  # Part II: close all file decriptors (except for STDIN/STDOUT/STDERR)
  ios = Array.new(8192) {|i| IO.for_fd(i) rescue nil}.compact
  ios.each do |io|
    next if io.fileno < 3
    io.close
  end
end

.daemonize(logfile_name = nil, app_name = nil) ⇒ Object

Transform the current process into a daemon



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/daemons/daemonize.rb', line 88

def daemonize(logfile_name = nil, app_name = nil)
  # Split rand streams between spawning and daemonized process
  srand 
  
   # Fork and exit from the parent
  safefork and exit

  # 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 = safefork
  
  $0 = app_name if app_name
  
  # Release old working directory
  Dir.chdir "/"  

  close_io()

  redirect_io(logfile_name)
  
  return sess_id
end

.redirect_io(logfile_name) ⇒ Object

Free STDIN/STDOUT/STDERR file descriptors and point them somewhere sensible



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/daemons/daemonize.rb', line 145

def redirect_io(logfile_name)
  begin; STDIN.reopen "/dev/null"; rescue ::Exception; end       
   
  if logfile_name
    begin
      STDOUT.reopen logfile_name, "a" 
      File.chmod(0644, logfile_name)
      STDOUT.sync = true
    rescue ::Exception
      begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
    end
  else
    begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
  end
  
  begin; STDERR.reopen STDOUT; rescue ::Exception; end
  STDERR.sync = true
end

.safeforkObject

Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/daemons/daemonize.rb', line 5

def safefork
  tryagain = true

  while tryagain
    tryagain = false
    begin
      if pid = fork
        return pid
      end
    rescue Errno::EWOULDBLOCK
      sleep 5
      tryagain = true
    end
  end
end

.simulate(logfile_name = nil) ⇒ Object

Simulate the daemonization process (:ontop mode) NOTE: STDOUT and STDERR will not be redirected to the logfile, because in :ontop mode, we normally want to see the output



26
27
28
29
30
31
32
33
34
# File 'lib/daemons/daemonize.rb', line 26

def simulate(logfile_name = nil)
  # Release old working directory
  Dir.chdir "/"   

  close_io()

  # Free STDIN and point them somewhere sensible
  begin; STDIN.reopen "/dev/null"; rescue ::Exception; end       
end