mlanett-daemons 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +17 -0
  4. data/LICENSE +29 -0
  5. data/README +223 -0
  6. data/Rakefile +15 -0
  7. data/Releases +126 -0
  8. data/TODO +6 -0
  9. data/daemons.gemspec +27 -0
  10. data/daemons.tmproj +56 -0
  11. data/examples/call/call.rb +56 -0
  12. data/examples/call/call.rb.log +1 -0
  13. data/examples/call/call_monitor.rb +55 -0
  14. data/examples/daemonize/daemonize.rb +20 -0
  15. data/examples/run/ctrl_crash.rb +17 -0
  16. data/examples/run/ctrl_exec.rb +16 -0
  17. data/examples/run/ctrl_exit.rb +15 -0
  18. data/examples/run/ctrl_keep_pid_files.rb +17 -0
  19. data/examples/run/ctrl_monitor.rb +16 -0
  20. data/examples/run/ctrl_multiple.rb +16 -0
  21. data/examples/run/ctrl_normal.rb +12 -0
  22. data/examples/run/ctrl_ontop.rb +16 -0
  23. data/examples/run/ctrl_optionparser.rb +43 -0
  24. data/examples/run/ctrl_proc.rb +25 -0
  25. data/examples/run/ctrl_proc.rb.output +101 -0
  26. data/examples/run/ctrl_proc_multiple.rb +22 -0
  27. data/examples/run/ctrl_proc_multiple.rb.output +2 -0
  28. data/examples/run/ctrl_proc_simple.rb +17 -0
  29. data/examples/run/myserver.rb +12 -0
  30. data/examples/run/myserver_crashing.rb +14 -0
  31. data/examples/run/myserver_crashing.rb.output +30 -0
  32. data/examples/run/myserver_exiting.rb +8 -0
  33. data/html/classes/Daemonize.html +497 -0
  34. data/html/classes/Daemons.html +683 -0
  35. data/html/classes/Daemons/Application.html +836 -0
  36. data/html/classes/Daemons/ApplicationGroup.html +508 -0
  37. data/html/classes/Daemons/CmdException.html +113 -0
  38. data/html/classes/Daemons/Controller.html +429 -0
  39. data/html/classes/Daemons/Error.html +113 -0
  40. data/html/classes/Daemons/Exception.html +111 -0
  41. data/html/classes/Daemons/Monitor.html +263 -0
  42. data/html/classes/Daemons/Optparse.html +244 -0
  43. data/html/classes/Daemons/Pid.html +339 -0
  44. data/html/classes/Daemons/PidFile.html +441 -0
  45. data/html/classes/Daemons/PidMem.html +126 -0
  46. data/html/classes/Daemons/RuntimeException.html +113 -0
  47. data/html/classes/Daemons/SystemError.html +163 -0
  48. data/html/created.rid +1 -0
  49. data/html/files/README.html +377 -0
  50. data/html/files/Releases.html +342 -0
  51. data/html/files/TODO.html +121 -0
  52. data/html/files/lib/daemons/application_group_rb.html +101 -0
  53. data/html/files/lib/daemons/application_rb.html +110 -0
  54. data/html/files/lib/daemons/cmdline_rb.html +101 -0
  55. data/html/files/lib/daemons/controller_rb.html +101 -0
  56. data/html/files/lib/daemons/daemonize_rb.html +207 -0
  57. data/html/files/lib/daemons/exceptions_rb.html +101 -0
  58. data/html/files/lib/daemons/monitor_rb.html +108 -0
  59. data/html/files/lib/daemons/pid_rb.html +108 -0
  60. data/html/files/lib/daemons/pidfile_rb.html +108 -0
  61. data/html/files/lib/daemons/pidmem_rb.html +108 -0
  62. data/html/files/lib/daemons_rb.html +117 -0
  63. data/html/fr_class_index.html +41 -0
  64. data/html/fr_file_index.html +40 -0
  65. data/html/fr_method_index.html +91 -0
  66. data/html/index.html +24 -0
  67. data/html/rdoc-style.css +208 -0
  68. data/lib/daemons.rb +284 -0
  69. data/lib/daemons/application.rb +376 -0
  70. data/lib/daemons/application_group.rb +152 -0
  71. data/lib/daemons/cmdline.rb +117 -0
  72. data/lib/daemons/controller.rb +137 -0
  73. data/lib/daemons/daemonize.rb +263 -0
  74. data/lib/daemons/exceptions.rb +28 -0
  75. data/lib/daemons/monitor.rb +136 -0
  76. data/lib/daemons/pid.rb +115 -0
  77. data/lib/daemons/pidfile.rb +111 -0
  78. data/lib/daemons/pidmem.rb +10 -0
  79. data/lib/daemons/version.rb +3 -0
  80. data/setup.rb +1360 -0
  81. data/test/call_as_daemon.rb +12 -0
  82. data/test/tc_main.rb +24 -0
  83. data/test/test1.rb +19 -0
  84. data/test/testapp.rb +11 -0
  85. metadata +170 -0
@@ -0,0 +1,22 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ options = {
13
+ :log_output => true,
14
+ :multiple => true,
15
+ }
16
+
17
+
18
+ Daemons.run_proc('ctrl_proc_multiple.rb', options) do
19
+ puts "hello"
20
+ sleep(5)
21
+ puts "done"
22
+ end
@@ -0,0 +1,2 @@
1
+
2
+ hello
@@ -0,0 +1,17 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))
2
+
3
+ if File.exist?(File.join(lib_dir, 'daemons.rb'))
4
+ $LOAD_PATH.unshift lib_dir
5
+ else
6
+ begin; require 'rubygems'; rescue ::Exception; end
7
+ end
8
+
9
+ require 'daemons'
10
+
11
+
12
+ Daemons.run_proc('ctrl_proc_simple.rb') do
13
+ loop do
14
+ puts 'ping from proc!'
15
+ sleep(3)
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ # This is myserver.rb, an example server that is to be controlled by daemons
5
+ # and that does nothing really useful at the moment.
6
+ #
7
+ # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts.
8
+
9
+ loop do
10
+ puts 'ping from myserver.rb!'
11
+ sleep(3)
12
+ end
@@ -0,0 +1,14 @@
1
+ # This is myserver.rb, an example server that is to be controlled by daemons
2
+ # and that does nothing really useful at the moment.
3
+ #
4
+ # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts.
5
+
6
+ loop do
7
+ puts 'ping from myserver.rb!'
8
+ puts 'this example server will crash in 3 seconds...'
9
+
10
+ sleep(3)
11
+
12
+ puts 'CRASH!'
13
+ raise 'CRASH!'
14
+ end
@@ -0,0 +1,30 @@
1
+ /home/uehli/Desktop/daemons-current/examples/myserver_crashing.rb:13: CRASH! (RuntimeError)
2
+ from /home/uehli/Desktop/daemons-current/examples/myserver_crashing.rb:6:in `loop'
3
+ from /home/uehli/Desktop/daemons-current/examples/myserver_crashing.rb:6
4
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:116:in `load'
5
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:116:in `run_via_load'
6
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:90:in `start'
7
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:359:in `run'
8
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:469:in `run'
9
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:468:in `call'
10
+ from /home/uehli/Desktop/daemons-current/lib/daemons/cmdline.rb:94:in `catch_exceptions'
11
+ from /home/uehli/Desktop/daemons-current/lib/daemons.rb:468:in `run'
12
+ from ctrl_crash.rb:17
13
+ ping from myserver.rb!
14
+ this example server will crash in 3 seconds...
15
+ CRASH!
16
+ ping from myserver.rb!
17
+ this example server will crash in 3 seconds...
18
+ CRASH!
19
+ /Users/uehli/Projects/daemons-proj/examples/run/myserver_crashing.rb:13: CRASH! (RuntimeError)
20
+ from /Users/uehli/Projects/daemons-proj/examples/run/myserver_crashing.rb:6:in `loop'
21
+ from /Users/uehli/Projects/daemons-proj/examples/run/myserver_crashing.rb:6
22
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/application.rb:176:in `load'
23
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/application.rb:176:in `start_load'
24
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/application.rb:257:in `start'
25
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/controller.rb:69:in `run'
26
+ from /Users/uehli/Projects/daemons-proj/lib/daemons.rb:139:in `run'
27
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/cmdline.rb:105:in `call'
28
+ from /Users/uehli/Projects/daemons-proj/lib/daemons/cmdline.rb:105:in `catch_exceptions'
29
+ from /Users/uehli/Projects/daemons-proj/lib/daemons.rb:138:in `run'
30
+ from ctrl_crash.rb:17
@@ -0,0 +1,8 @@
1
+ loop do
2
+ puts 'ping from myserver.rb!'
3
+ puts 'this example server will exit in 3 seconds...'
4
+
5
+ sleep(3)
6
+
7
+ Process.exit
8
+ end
@@ -0,0 +1,497 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: Daemonize</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">Daemonize</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/daemons/daemonize_rb.html">
59
+ lib/daemons/daemonize.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+ <div id="description">
76
+ <h1><a href="Daemonize.html">Daemonize</a> Library</h1>
77
+ <p>
78
+ February. 4, 2005 Travis Whitton &lt;whitton@atlantic.net&gt;
79
+ </p>
80
+ <p>
81
+ <a href="Daemonize.html">Daemonize</a> allows you to easily modify any
82
+ existing Ruby program to run as a daemon. See README.rdoc for more details.
83
+ </p>
84
+ <h2>How to install</h2>
85
+ <ol>
86
+ <li>su to root
87
+
88
+ </li>
89
+ <li>ruby install.rb
90
+
91
+ </li>
92
+ </ol>
93
+ <p>
94
+ build the docs if you want to
95
+ </p>
96
+ <ol>
97
+ <li>rdoc &#8212;main README.rdoc daemonize.rb README.rdoc
98
+
99
+ </li>
100
+ </ol>
101
+ <h2>Copying</h2>
102
+ <p>
103
+ The <a href="Daemonize.html">Daemonize</a> extension module is copywrited
104
+ free software by Travis Whitton &lt;whitton@atlantic.net&gt;. You can
105
+ redistribute it under the terms specified in the COPYING file of the Ruby
106
+ distribution.
107
+ </p>
108
+ <h2>WARRANTY</h2>
109
+ <p>
110
+ THIS SOFTWARE IS PROVIDED &quot;AS IS&quot; AND WITHOUT ANY EXPRESS OR
111
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
112
+ OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
113
+ </p>
114
+ <hr size="2"></hr><h2>Purpose</h2>
115
+ <p>
116
+ <a href="Daemonize.html">Daemonize</a> is a module derived from
117
+ Perl&#8216;s Proc::Daemon module. This module allows you to easily modify
118
+ any existing Ruby program to run as a daemon. A daemon is a process that
119
+ runs in the background with no controlling terminal. Generally servers
120
+ (like FTP and HTTP servers) run as daemon processes. Note, do not make the
121
+ mistake that a daemon == server. Converting a program to a daemon by hand
122
+ is a relatively simple process; however, this module will save you the
123
+ effort of repeatedly looking up the procedure, and it will also insure that
124
+ your programs are daemonized in the safest and most corrects fashion
125
+ possible.
126
+ </p>
127
+ <h2>Procedure</h2>
128
+ <p>
129
+ The <a href="Daemonize.html">Daemonize</a> module does the following:
130
+ </p>
131
+ <p>
132
+ Forks a child and exits the parent process.
133
+ </p>
134
+ <p>
135
+ Becomes a session leader (which detaches the program from the controlling
136
+ terminal).
137
+ </p>
138
+ <p>
139
+ Forks another child process and exits first child. This prevents the
140
+ potential of acquiring a controlling terminal.
141
+ </p>
142
+ <p>
143
+ Changes the current working directory to &quot;/&quot;.
144
+ </p>
145
+ <p>
146
+ Clears the file creation mask.
147
+ </p>
148
+ <p>
149
+ Closes file descriptors.
150
+ </p>
151
+ <h2>Example usage</h2>
152
+ <p>
153
+ Using the <a href="Daemonize.html">Daemonize</a> module is extremely
154
+ simple:
155
+ </p>
156
+ <pre>
157
+ require 'daemonize'
158
+
159
+ class TestDaemon
160
+ include Daemonize
161
+
162
+ def initialize
163
+ daemonize()
164
+ loop do
165
+ # do some work here
166
+ end
167
+ end
168
+ end
169
+ </pre>
170
+ <h2>Credits</h2>
171
+ <p>
172
+ <a href="Daemonize.html">Daemonize</a> was written by Travis Whitton and is
173
+ based on Perl&#8216;s Proc::Daemonize, which was written by Earl Hood. The
174
+ above documentation is also partially borrowed from the Proc::Daemonize POD
175
+ documentation.
176
+ </p>
177
+
178
+ </div>
179
+
180
+
181
+ </div>
182
+
183
+ <div id="method-list">
184
+ <h3 class="section-bar">Methods</h3>
185
+
186
+ <div class="name-list">
187
+ <a href="#M000063">call_as_daemon</a>&nbsp;&nbsp;
188
+ <a href="#M000064">daemonize</a>&nbsp;&nbsp;
189
+ <a href="#M000065">redirect_io</a>&nbsp;&nbsp;
190
+ <a href="#M000061">safefork</a>&nbsp;&nbsp;
191
+ <a href="#M000062">simulate</a>&nbsp;&nbsp;
192
+ </div>
193
+ </div>
194
+
195
+ </div>
196
+
197
+
198
+ <!-- if includes -->
199
+
200
+ <div id="section">
201
+
202
+
203
+ <div id="constants-list">
204
+ <h3 class="section-bar">Constants</h3>
205
+
206
+ <div class="name-list">
207
+ <table summary="Constants">
208
+ <tr class="top-aligned-row context-row">
209
+ <td class="context-item-name">VERSION</td>
210
+ <td>=</td>
211
+ <td class="context-item-value">&quot;0.1.1m&quot;</td>
212
+ </tr>
213
+ </table>
214
+ </div>
215
+ </div>
216
+
217
+
218
+
219
+
220
+
221
+
222
+ <!-- if method_list -->
223
+ <div id="methods">
224
+ <h3 class="section-bar">Public Instance methods</h3>
225
+
226
+ <div id="method-M000063" class="method-detail">
227
+ <a name="M000063"></a>
228
+
229
+ <div class="method-heading">
230
+ <a href="#M000063" class="method-signature">
231
+ <span class="method-name">call_as_daemon</span><span class="method-args">(block, logfile_name = nil, app_name = nil)</span>
232
+ </a>
233
+ </div>
234
+
235
+ <div class="method-description">
236
+ <p><a class="source-toggle" href="#"
237
+ onclick="toggleCode('M000063-source');return false;">[Source]</a></p>
238
+ <div class="method-source-code" id="M000063-source">
239
+ <pre>
240
+ <span class="ruby-comment cmt"># File lib/daemons/daemonize.rb, line 142</span>
241
+ 142: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call_as_daemon</span>(<span class="ruby-identifier">block</span>, <span class="ruby-identifier">logfile_name</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">app_name</span> = <span class="ruby-keyword kw">nil</span>)
242
+ 143: <span class="ruby-identifier">rd</span>, <span class="ruby-identifier">wr</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
243
+ 144:
244
+ 145: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">tmppid</span> = <span class="ruby-identifier">safefork</span>
245
+ 146: <span class="ruby-comment cmt"># parent</span>
246
+ 147: <span class="ruby-identifier">wr</span>.<span class="ruby-identifier">close</span>
247
+ 148: <span class="ruby-identifier">pid</span> = <span class="ruby-identifier">rd</span>.<span class="ruby-identifier">read</span>.<span class="ruby-identifier">to_i</span>
248
+ 149: <span class="ruby-identifier">rd</span>.<span class="ruby-identifier">close</span>
249
+ 150:
250
+ 151: <span class="ruby-constant">Process</span>.<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">tmppid</span>)
251
+ 152:
252
+ 153: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">pid</span>
253
+ 154: <span class="ruby-keyword kw">else</span>
254
+ 155: <span class="ruby-comment cmt"># child</span>
255
+ 156:
256
+ 157: <span class="ruby-identifier">rd</span>.<span class="ruby-identifier">close</span>
257
+ 158:
258
+ 159: <span class="ruby-comment cmt"># Detach from the controlling terminal</span>
259
+ 160: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">sess_id</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
260
+ 161: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Daemons</span>.<span class="ruby-constant">RuntimeException</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'cannot detach from controlling terminal'</span>)
261
+ 162: <span class="ruby-keyword kw">end</span>
262
+ 163:
263
+ 164: <span class="ruby-comment cmt"># Prevent the possibility of acquiring a controlling terminal</span>
264
+ 165: <span class="ruby-comment cmt">#if oldmode.zero?</span>
265
+ 166: <span class="ruby-identifier">trap</span> <span class="ruby-value str">'SIGHUP'</span>, <span class="ruby-value str">'IGNORE'</span>
266
+ 167: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">pid</span> = <span class="ruby-identifier">safefork</span>
267
+ 168: <span class="ruby-comment cmt">#end</span>
268
+ 169:
269
+ 170: <span class="ruby-identifier">wr</span>.<span class="ruby-identifier">write</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">pid</span>
270
+ 171: <span class="ruby-identifier">wr</span>.<span class="ruby-identifier">close</span>
271
+ 172:
272
+ 173: <span class="ruby-identifier">$0</span> = <span class="ruby-identifier">app_name</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">app_name</span>
273
+ 174:
274
+ 175: <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">chdir</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-comment cmt"># Release old working directory</span>
275
+ 176: <span class="ruby-constant">File</span>.<span class="ruby-identifier">umask</span> <span class="ruby-value">0000</span> <span class="ruby-comment cmt"># Insure sensible umask</span>
276
+ 177:
277
+ 178: <span class="ruby-comment cmt"># Make sure all file descriptors are closed</span>
278
+ 179: <span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">each_object</span>(<span class="ruby-constant">IO</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
279
+ 180: <span class="ruby-keyword kw">unless</span> [<span class="ruby-constant">STDIN</span>, <span class="ruby-constant">STDOUT</span>, <span class="ruby-constant">STDERR</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">io</span>)
280
+ 181: <span class="ruby-keyword kw">begin</span>
281
+ 182: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">closed?</span>
282
+ 183: <span class="ruby-identifier">io</span>.<span class="ruby-identifier">close</span>
283
+ 184: <span class="ruby-keyword kw">end</span>
284
+ 185: <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>
285
+ 186: <span class="ruby-keyword kw">end</span>
286
+ 187: <span class="ruby-keyword kw">end</span>
287
+ 188: <span class="ruby-keyword kw">end</span>
288
+ 189:
289
+ 190: <span class="ruby-identifier">redirect_io</span>(<span class="ruby-identifier">logfile_name</span>)
290
+ 191:
291
+ 192: <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>
292
+ 193:
293
+ 194: <span class="ruby-identifier">exit</span>
294
+ 195: <span class="ruby-keyword kw">end</span>
295
+ 196: <span class="ruby-keyword kw">end</span>
296
+ </pre>
297
+ </div>
298
+ </div>
299
+ </div>
300
+
301
+ <div id="method-M000064" class="method-detail">
302
+ <a name="M000064"></a>
303
+
304
+ <div class="method-heading">
305
+ <a href="#M000064" class="method-signature">
306
+ <span class="method-name">daemonize</span><span class="method-args">(logfile_name = nil, app_name = nil)</span>
307
+ </a>
308
+ </div>
309
+
310
+ <div class="method-description">
311
+ <p>
312
+ This method causes the current running process to become a daemon
313
+ </p>
314
+ <p><a class="source-toggle" href="#"
315
+ onclick="toggleCode('M000064-source');return false;">[Source]</a></p>
316
+ <div class="method-source-code" id="M000064-source">
317
+ <pre>
318
+ <span class="ruby-comment cmt"># File lib/daemons/daemonize.rb, line 201</span>
319
+ 201: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">daemonize</span>(<span class="ruby-identifier">logfile_name</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">app_name</span> = <span class="ruby-keyword kw">nil</span>)
320
+ 202: <span class="ruby-identifier">srand</span> <span class="ruby-comment cmt"># Split rand streams between spawning and daemonized process</span>
321
+ 203: <span class="ruby-identifier">safefork</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">exit</span> <span class="ruby-comment cmt"># Fork and exit from the parent</span>
322
+ 204:
323
+ 205: <span class="ruby-comment cmt"># Detach from the controlling terminal</span>
324
+ 206: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">sess_id</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
325
+ 207: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Daemons</span>.<span class="ruby-constant">RuntimeException</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'cannot detach from controlling terminal'</span>)
326
+ 208: <span class="ruby-keyword kw">end</span>
327
+ 209:
328
+ 210: <span class="ruby-comment cmt"># Prevent the possibility of acquiring a controlling terminal</span>
329
+ 211: <span class="ruby-comment cmt">#if oldmode.zero?</span>
330
+ 212: <span class="ruby-identifier">trap</span> <span class="ruby-value str">'SIGHUP'</span>, <span class="ruby-value str">'IGNORE'</span>
331
+ 213: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">pid</span> = <span class="ruby-identifier">safefork</span>
332
+ 214: <span class="ruby-comment cmt">#end</span>
333
+ 215:
334
+ 216: <span class="ruby-identifier">$0</span> = <span class="ruby-identifier">app_name</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">app_name</span>
335
+ 217:
336
+ 218: <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">chdir</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-comment cmt"># Release old working directory</span>
337
+ 219: <span class="ruby-constant">File</span>.<span class="ruby-identifier">umask</span> <span class="ruby-value">0000</span> <span class="ruby-comment cmt"># Insure sensible umask</span>
338
+ 220:
339
+ 221: <span class="ruby-comment cmt"># Make sure all file descriptors are closed</span>
340
+ 222: <span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">each_object</span>(<span class="ruby-constant">IO</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
341
+ 223: <span class="ruby-keyword kw">unless</span> [<span class="ruby-constant">STDIN</span>, <span class="ruby-constant">STDOUT</span>, <span class="ruby-constant">STDERR</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">io</span>)
342
+ 224: <span class="ruby-keyword kw">begin</span>
343
+ 225: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">closed?</span>
344
+ 226: <span class="ruby-identifier">io</span>.<span class="ruby-identifier">close</span>
345
+ 227: <span class="ruby-keyword kw">end</span>
346
+ 228: <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>
347
+ 229: <span class="ruby-keyword kw">end</span>
348
+ 230: <span class="ruby-keyword kw">end</span>
349
+ 231: <span class="ruby-keyword kw">end</span>
350
+ 232:
351
+ 233: <span class="ruby-identifier">redirect_io</span>(<span class="ruby-identifier">logfile_name</span>)
352
+ 234:
353
+ 235: <span class="ruby-comment cmt">#return oldmode ? sess_id : 0 # Return value is mostly irrelevant</span>
354
+ 236: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">sess_id</span>
355
+ 237: <span class="ruby-keyword kw">end</span>
356
+ </pre>
357
+ </div>
358
+ </div>
359
+ </div>
360
+
361
+ <div id="method-M000065" class="method-detail">
362
+ <a name="M000065"></a>
363
+
364
+ <div class="method-heading">
365
+ <a href="#M000065" class="method-signature">
366
+ <span class="method-name">redirect_io</span><span class="method-args">(logfile_name)</span>
367
+ </a>
368
+ </div>
369
+
370
+ <div class="method-description">
371
+ <p>
372
+ Free file descriptors and point them somewhere sensible STDOUT/STDERR
373
+ should go to a logfile
374
+ </p>
375
+ <p><a class="source-toggle" href="#"
376
+ onclick="toggleCode('M000065-source');return false;">[Source]</a></p>
377
+ <div class="method-source-code" id="M000065-source">
378
+ <pre>
379
+ <span class="ruby-comment cmt"># File lib/daemons/daemonize.rb, line 244</span>
380
+ 244: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">redirect_io</span>(<span class="ruby-identifier">logfile_name</span>)
381
+ 245: <span class="ruby-keyword kw">begin</span>; <span class="ruby-constant">STDIN</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>; <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>; <span class="ruby-keyword kw">end</span>
382
+ 246:
383
+ 247: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">logfile_name</span>
384
+ 248: <span class="ruby-keyword kw">begin</span>
385
+ 249: <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-identifier">logfile_name</span>, <span class="ruby-value str">&quot;a&quot;</span>
386
+ 250: <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword kw">true</span>
387
+ 251: <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>
388
+ 252: <span class="ruby-keyword kw">begin</span>; <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>; <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>; <span class="ruby-keyword kw">end</span>
389
+ 253: <span class="ruby-keyword kw">end</span>
390
+ 254: <span class="ruby-keyword kw">else</span>
391
+ 255: <span class="ruby-keyword kw">begin</span>; <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>; <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>; <span class="ruby-keyword kw">end</span>
392
+ 256: <span class="ruby-keyword kw">end</span>
393
+ 257:
394
+ 258: <span class="ruby-keyword kw">begin</span>; <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-constant">STDOUT</span>; <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>; <span class="ruby-keyword kw">end</span>
395
+ 259: <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">sync</span> = <span class="ruby-keyword kw">true</span>
396
+ 260: <span class="ruby-keyword kw">end</span>
397
+ </pre>
398
+ </div>
399
+ </div>
400
+ </div>
401
+
402
+ <div id="method-M000061" class="method-detail">
403
+ <a name="M000061"></a>
404
+
405
+ <div class="method-heading">
406
+ <a href="#M000061" class="method-signature">
407
+ <span class="method-name">safefork</span><span class="method-args">()</span>
408
+ </a>
409
+ </div>
410
+
411
+ <div class="method-description">
412
+ <p>
413
+ Try to fork if at all possible retrying every 5 sec if the maximum process
414
+ limit for the system has been reached
415
+ </p>
416
+ <p><a class="source-toggle" href="#"
417
+ onclick="toggleCode('M000061-source');return false;">[Source]</a></p>
418
+ <div class="method-source-code" id="M000061-source">
419
+ <pre>
420
+ <span class="ruby-comment cmt"># File lib/daemons/daemonize.rb, line 97</span>
421
+ 97: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">safefork</span>
422
+ 98: <span class="ruby-identifier">tryagain</span> = <span class="ruby-keyword kw">true</span>
423
+ 99:
424
+ 100: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">tryagain</span>
425
+ 101: <span class="ruby-identifier">tryagain</span> = <span class="ruby-keyword kw">false</span>
426
+ 102: <span class="ruby-keyword kw">begin</span>
427
+ 103: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">pid</span> = <span class="ruby-identifier">fork</span>
428
+ 104: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">pid</span>
429
+ 105: <span class="ruby-keyword kw">end</span>
430
+ 106: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EWOULDBLOCK</span>
431
+ 107: <span class="ruby-identifier">sleep</span> <span class="ruby-value">5</span>
432
+ 108: <span class="ruby-identifier">tryagain</span> = <span class="ruby-keyword kw">true</span>
433
+ 109: <span class="ruby-keyword kw">end</span>
434
+ 110: <span class="ruby-keyword kw">end</span>
435
+ 111: <span class="ruby-keyword kw">end</span>
436
+ </pre>
437
+ </div>
438
+ </div>
439
+ </div>
440
+
441
+ <div id="method-M000062" class="method-detail">
442
+ <a name="M000062"></a>
443
+
444
+ <div class="method-heading">
445
+ <a href="#M000062" class="method-signature">
446
+ <span class="method-name">simulate</span><span class="method-args">(logfile_name = nil)</span>
447
+ </a>
448
+ </div>
449
+
450
+ <div class="method-description">
451
+ <p><a class="source-toggle" href="#"
452
+ onclick="toggleCode('M000062-source');return false;">[Source]</a></p>
453
+ <div class="method-source-code" id="M000062-source">
454
+ <pre>
455
+ <span class="ruby-comment cmt"># File lib/daemons/daemonize.rb, line 115</span>
456
+ 115: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">simulate</span>(<span class="ruby-identifier">logfile_name</span> = <span class="ruby-keyword kw">nil</span>)
457
+ 116: <span class="ruby-comment cmt"># NOTE: STDOUT and STDERR will not be redirected to the logfile, because in :ontop mode, we normally want to see the output</span>
458
+ 117:
459
+ 118: <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">chdir</span> <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-comment cmt"># Release old working directory</span>
460
+ 119: <span class="ruby-constant">File</span>.<span class="ruby-identifier">umask</span> <span class="ruby-value">0000</span> <span class="ruby-comment cmt"># Insure sensible umask</span>
461
+ 120:
462
+ 121: <span class="ruby-comment cmt"># Make sure all file descriptors are closed</span>
463
+ 122: <span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">each_object</span>(<span class="ruby-constant">IO</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
464
+ 123: <span class="ruby-keyword kw">unless</span> [<span class="ruby-constant">STDIN</span>, <span class="ruby-constant">STDOUT</span>, <span class="ruby-constant">STDERR</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">io</span>)
465
+ 124: <span class="ruby-keyword kw">begin</span>
466
+ 125: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">closed?</span>
467
+ 126: <span class="ruby-identifier">io</span>.<span class="ruby-identifier">close</span>
468
+ 127: <span class="ruby-keyword kw">end</span>
469
+ 128: <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>
470
+ 129: <span class="ruby-keyword kw">end</span>
471
+ 130: <span class="ruby-keyword kw">end</span>
472
+ 131: <span class="ruby-keyword kw">end</span>
473
+ 132:
474
+ 133: <span class="ruby-comment cmt"># Free file descriptors and</span>
475
+ 134: <span class="ruby-comment cmt"># point them somewhere sensible</span>
476
+ 135: <span class="ruby-comment cmt"># STDOUT/STDERR should go to a logfile</span>
477
+ 136:
478
+ 137: <span class="ruby-keyword kw">begin</span>; <span class="ruby-constant">STDIN</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>; <span class="ruby-keyword kw">rescue</span> <span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>; <span class="ruby-keyword kw">end</span>
479
+ 138: <span class="ruby-keyword kw">end</span>
480
+ </pre>
481
+ </div>
482
+ </div>
483
+ </div>
484
+
485
+
486
+ </div>
487
+
488
+
489
+ </div>
490
+
491
+
492
+ <div id="validator-badges">
493
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
494
+ </div>
495
+
496
+ </body>
497
+ </html>