Module: Daemonize
- Defined in:
- lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb
Constant Summary collapse
- VERSION =
"0.1.1m"
Class Method Summary collapse
- .call_as_daemon(block, logfile_name = nil, app_name = nil) ⇒ Object
-
.daemonize(logfile_name = nil, app_name = nil) ⇒ Object
This method causes the current running process to become a daemon.
-
.redirect_io(logfile_name) ⇒ Object
Free file descriptors and point them somewhere sensible STDOUT/STDERR should go to a logfile.
-
.safefork ⇒ Object
Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached.
- .simulate(logfile_name = nil) ⇒ Object
Class Method Details
.call_as_daemon(block, logfile_name = nil, app_name = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb', line 142 def call_as_daemon(block, logfile_name = nil, app_name = nil) rd, wr = IO.pipe if tmppid = safefork # parent wr.close pid = rd.read.to_i rd.close Process.waitpid(tmppid) return pid else # 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 #if oldmode.zero? trap 'SIGHUP', 'IGNORE' exit if pid = safefork #end wr.write Process.pid wr.close $0 = app_name if app_name Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin unless io.closed? io.close end rescue ::Exception end end end redirect_io(logfile_name) block.call exit end end |
.daemonize(logfile_name = nil, app_name = nil) ⇒ Object
This method causes the current running process to become a daemon
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb', line 201 def daemonize(logfile_name = nil, app_name = nil) srand # Split rand streams between spawning and daemonized process safefork and 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 #if oldmode.zero? trap 'SIGHUP', 'IGNORE' exit if pid = safefork #end $0 = app_name if app_name Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin unless io.closed? io.close end rescue ::Exception end end end redirect_io(logfile_name) #return oldmode ? sess_id : 0 # Return value is mostly irrelevant return sess_id end |
.redirect_io(logfile_name) ⇒ Object
Free file descriptors and point them somewhere sensible STDOUT/STDERR should go to a logfile
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb', line 244 def redirect_io(logfile_name) begin; STDIN.reopen "/dev/null"; rescue ::Exception; end if logfile_name begin STDOUT.reopen logfile_name, "a" 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 |
.safefork ⇒ Object
Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb', line 97 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gems/daemons-1.0.10/lib/daemons/daemonize.rb', line 115 def simulate(logfile_name = nil) # NOTE: STDOUT and STDERR will not be redirected to the logfile, because in :ontop mode, we normally want to see the output Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin unless io.closed? io.close end rescue ::Exception end end end # Free file descriptors and # point them somewhere sensible # STDOUT/STDERR should go to a logfile begin; STDIN.reopen "/dev/null"; rescue ::Exception; end end |