Module: Daemonize
- Defined in:
- lib/feed_updater/vendor/daemons/daemonize.rb
Constant Summary collapse
- VERSION =
"0.1.1m"
Class Method Summary collapse
- .call_as_daemon(block, logfile_name = nil, oldmode = 0) ⇒ Object
-
.daemonize(logfile_name = nil, oldmode = 0) ⇒ Object
This method causes the current running process to become a daemon.
-
.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, oldmode = 0) ⇒ 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 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/feed_updater/vendor/daemons/daemonize.rb', line 142 def call_as_daemon(block, logfile_name = nil, oldmode = 0) rd, wr = IO.pipe if tmppid = safefork # parent wr.close pid = rd.read.to_i rd.close Process.waitpid(tmppid) return pid else 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 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 STDIN.reopen "/dev/null" rescue nil if logfile_name begin STDOUT.reopen logfile_name, "a" rescue ::Exception STDOUT.reopen "/dev/null" rescue nil end else STDOUT.reopen "/dev/null" rescue nil end STDERR.reopen STDOUT rescue nil block.call exit end end |
.daemonize(logfile_name = nil, oldmode = 0) ⇒ Object
This method causes the current running process to become a daemon
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/feed_updater/vendor/daemons/daemonize.rb', line 213 def daemonize(logfile_name = nil, oldmode=0) 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 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 STDIN.reopen "/dev/null" rescue nil if logfile_name begin STDOUT.reopen logfile_name, "a" rescue ::Exception STDOUT.reopen "/dev/null" rescue nil end else STDOUT.reopen "/dev/null" rescue nil end STDERR.reopen STDOUT rescue nil return oldmode ? sess_id : 0 # Return value is mostly irrelevant 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/feed_updater/vendor/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/feed_updater/vendor/daemons/daemonize.rb', line 115 def simulate(logfile_name = nil) # NOTE: STDOUT and STDERR will not be redirected to the logfile! 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 STDIN.reopen "/dev/null" rescue nil end |