daemons 1.1.6 → 1.1.8
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.
- data/LICENSE +0 -7
- data/README +2 -12
- data/Releases +10 -0
- data/examples/run/ctrl_slowstop.rb +16 -0
- data/examples/run/myserver_slowstop.rb +21 -0
- data/lib/daemons.rb +1 -1
- data/lib/daemons/application.rb +6 -4
- data/lib/daemons/daemonize.rb +50 -158
- data/lib/daemons/pid.rb +0 -1
- metadata +5 -3
data/LICENSE
CHANGED
@@ -20,10 +20,3 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
20
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
21
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
22
|
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
-
|
24
|
-
This license does not apply to daemonize.rb, which is was written by
|
25
|
-
Travis Whitton und published under the following license:
|
26
|
-
|
27
|
-
The Daemonize extension module is copywrited free software by Travis Whitton
|
28
|
-
<whitton@atlantic.net>. You can redistribute it under the terms specified in
|
29
|
-
the COPYING file of the Ruby distribution.
|
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= Daemons Version 1.1.
|
1
|
+
= Daemons Version 1.1.8
|
2
2
|
|
3
3
|
(See Releases for release-specific information)
|
4
4
|
|
@@ -14,9 +14,6 @@ Besides this basic functionality, daemons offers many advanced features like <i>
|
|
14
14
|
and logging (in case your ruby script crashes) and <i>monitoring</i> and automatic restarting of your processes
|
15
15
|
if they crash.
|
16
16
|
|
17
|
-
Daemons includes the <tt>daemonize.rb</tt> script written by <i>Travis Whitton</i> to do the daemonization
|
18
|
-
process.
|
19
|
-
|
20
17
|
== Basic Usage
|
21
18
|
|
22
19
|
You can use Daemons in four different ways:
|
@@ -189,7 +186,7 @@ Anonymous SVN checkout is available with "svn checkout http://daemons.rubyforge.
|
|
189
186
|
|
190
187
|
== License
|
191
188
|
|
192
|
-
Copyright (c) 2005-
|
189
|
+
Copyright (c) 2005-2012 Thomas Uehlinger
|
193
190
|
|
194
191
|
Permission is hereby granted, free of charge, to any person
|
195
192
|
obtaining a copy of this software and associated documentation
|
@@ -212,13 +209,6 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
212
209
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
213
210
|
OTHER DEALINGS IN THE SOFTWARE.
|
214
211
|
|
215
|
-
This license does not apply to daemonize.rb, which is was written by
|
216
|
-
Travis Whitton und published under the following license:
|
217
|
-
|
218
|
-
The Daemonize extension module is copywrited free software by Travis Whitton
|
219
|
-
<whitton@atlantic.net>. You can redistribute it under the terms specified in
|
220
|
-
the COPYING file of the Ruby distribution.
|
221
|
-
|
222
212
|
== Feedback and other resources
|
223
213
|
|
224
214
|
At http://rubyforge.org/projects/daemons.
|
data/Releases
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
= Daemons Release History
|
2
2
|
|
3
|
+
== Release 1.1.8: February 7, 2012
|
4
|
+
|
5
|
+
* rename to daemonization.rb to daemonize.rb (and Daemonization to Daemonize) to
|
6
|
+
ensure compatibility.
|
7
|
+
|
8
|
+
== Release 1.1.7: February 6, 2012
|
9
|
+
|
10
|
+
* start_proc: Write out the PID file in the newly created proc to avoid race conditions.
|
11
|
+
* daemonize.rb: remove to simplify licensing (replaced by daemonization.rb).
|
12
|
+
|
3
13
|
== Release 1.1.6: January 18, 2012
|
4
14
|
|
5
15
|
* Add the :app_name option for the "call" daemonization mode.
|
@@ -0,0 +1,16 @@
|
|
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
|
+
options = {
|
12
|
+
#:force_kill_waittime => 40
|
13
|
+
#:force_kill_waittime => -1 # do not wait before killing -9
|
14
|
+
}
|
15
|
+
|
16
|
+
Daemons.run(File.join(File.dirname(__FILE__), 'myserver_slowstop.rb'), options)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
# This is myserver_slowstop.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
|
+
trap('TERM') {
|
10
|
+
puts "received TERM"
|
11
|
+
|
12
|
+
# simulate the slow stopping
|
13
|
+
sleep(10)
|
14
|
+
|
15
|
+
exit
|
16
|
+
}
|
17
|
+
|
18
|
+
loop do
|
19
|
+
puts 'ping from myserver.rb!'
|
20
|
+
sleep(3)
|
21
|
+
end
|
data/lib/daemons.rb
CHANGED
data/lib/daemons/application.rb
CHANGED
@@ -206,6 +206,9 @@ module Daemons
|
|
206
206
|
return unless p = options[:proc]
|
207
207
|
|
208
208
|
myproc = proc do
|
209
|
+
|
210
|
+
@pid.pid = Process.pid
|
211
|
+
|
209
212
|
# We need this to remove the pid-file if the applications exits by itself.
|
210
213
|
# Note that <tt>at_text</tt> will only be run if the applications exits by calling
|
211
214
|
# <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
|
@@ -246,17 +249,17 @@ module Daemons
|
|
246
249
|
end
|
247
250
|
}
|
248
251
|
|
252
|
+
started()
|
253
|
+
|
249
254
|
p.call()
|
250
255
|
end
|
251
256
|
|
252
257
|
unless options[:ontop]
|
253
|
-
|
258
|
+
Daemonize.call_as_daemon(myproc, output_logfile, @group.app_name)
|
254
259
|
|
255
260
|
else
|
256
261
|
Daemonize.simulate(output_logfile)
|
257
262
|
|
258
|
-
@pid.pid = Process.pid
|
259
|
-
|
260
263
|
myproc.call
|
261
264
|
|
262
265
|
# why did we use this??
|
@@ -273,7 +276,6 @@ module Daemons
|
|
273
276
|
# end
|
274
277
|
end
|
275
278
|
|
276
|
-
started()
|
277
279
|
end
|
278
280
|
|
279
281
|
|
data/lib/daemons/daemonize.rb
CHANGED
@@ -1,97 +1,5 @@
|
|
1
|
-
#--
|
2
|
-
###############################################################################
|
3
|
-
# daemonize.rb is a slightly modified version of daemonize.rb was #
|
4
|
-
# from the Daemonize Library written by Travis Whitton #
|
5
|
-
# for details, read the notice below #
|
6
|
-
###############################################################################
|
7
|
-
#++
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# =Daemonize Library
|
11
|
-
#
|
12
|
-
# February. 4, 2005 Travis Whitton <whitton@atlantic.net>
|
13
|
-
#
|
14
|
-
# Daemonize allows you to easily modify any existing Ruby program to run
|
15
|
-
# as a daemon. See README.rdoc for more details.
|
16
|
-
#
|
17
|
-
# == How to install
|
18
|
-
# 1. su to root
|
19
|
-
# 2. ruby install.rb
|
20
|
-
# build the docs if you want to
|
21
|
-
# 3. rdoc --main README.rdoc daemonize.rb README.rdoc
|
22
|
-
#
|
23
|
-
# == Copying
|
24
|
-
# The Daemonize extension module is copywrited free software by Travis Whitton
|
25
|
-
# <whitton@atlantic.net>. You can redistribute it under the terms specified in
|
26
|
-
# the COPYING file of the Ruby distribution.
|
27
|
-
#
|
28
|
-
# == WARRANTY
|
29
|
-
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
30
|
-
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
31
|
-
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
32
|
-
# PURPOSE.
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# ----
|
36
|
-
#
|
37
|
-
# == Purpose
|
38
|
-
#
|
39
|
-
# Daemonize is a module derived from Perl's Proc::Daemon module. This module
|
40
|
-
# allows you to easily modify any existing Ruby program to run as a daemon.
|
41
|
-
# A daemon is a process that runs in the background with no controlling terminal.
|
42
|
-
# Generally servers (like FTP and HTTP servers) run as daemon processes.
|
43
|
-
# Note, do not make the mistake that a daemon == server. Converting a program
|
44
|
-
# to a daemon by hand is a relatively simple process; however, this module will
|
45
|
-
# save you the effort of repeatedly looking up the procedure, and it will also
|
46
|
-
# insure that your programs are daemonized in the safest and most corrects
|
47
|
-
# fashion possible.
|
48
|
-
#
|
49
|
-
# == Procedure
|
50
|
-
#
|
51
|
-
# The Daemonize module does the following:
|
52
|
-
#
|
53
|
-
# Forks a child and exits the parent process.
|
54
|
-
#
|
55
|
-
# Becomes a session leader (which detaches the program from
|
56
|
-
# the controlling terminal).
|
57
|
-
#
|
58
|
-
# Forks another child process and exits first child. This prevents
|
59
|
-
# the potential of acquiring a controlling terminal.
|
60
|
-
#
|
61
|
-
# Changes the current working directory to "/".
|
62
|
-
#
|
63
|
-
# Clears the file creation mask.
|
64
|
-
#
|
65
|
-
# Closes file descriptors.
|
66
|
-
#
|
67
|
-
# == Example usage
|
68
|
-
#
|
69
|
-
# Using the Daemonize module is extremely simple:
|
70
|
-
#
|
71
|
-
# require 'daemonize'
|
72
|
-
#
|
73
|
-
# class TestDaemon
|
74
|
-
# include Daemonize
|
75
|
-
#
|
76
|
-
# def initialize
|
77
|
-
# daemonize()
|
78
|
-
# loop do
|
79
|
-
# # do some work here
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
# end
|
83
|
-
#
|
84
|
-
# == Credits
|
85
|
-
#
|
86
|
-
# Daemonize was written by Travis Whitton and is based on Perl's
|
87
|
-
# Proc::Daemonize, which was written by Earl Hood. The above documentation
|
88
|
-
# is also partially borrowed from the Proc::Daemonize POD documentation.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
1
|
module Daemonize
|
93
|
-
|
94
|
-
|
2
|
+
|
95
3
|
# Try to fork if at all possible retrying every 5 sec if the
|
96
4
|
# maximum process limit for the system has been reached
|
97
5
|
def safefork
|
@@ -112,37 +20,29 @@ module Daemonize
|
|
112
20
|
module_function :safefork
|
113
21
|
|
114
22
|
|
23
|
+
# Simulate the daemonization process (:ontop mode)
|
24
|
+
# NOTE: STDOUT and STDERR will not be redirected to the logfile,
|
25
|
+
# because in :ontop mode, we normally want to see the output
|
115
26
|
def simulate(logfile_name = nil)
|
116
|
-
#
|
117
|
-
|
118
|
-
Dir.chdir "/" # Release old working directory
|
27
|
+
# Release old working directory
|
28
|
+
Dir.chdir "/"
|
119
29
|
|
120
|
-
|
121
|
-
ObjectSpace.each_object(IO) do |io|
|
122
|
-
unless [STDIN, STDOUT, STDERR].include?(io)
|
123
|
-
begin
|
124
|
-
unless io.closed?
|
125
|
-
io.close
|
126
|
-
end
|
127
|
-
rescue ::Exception
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
30
|
+
close_io()
|
131
31
|
|
132
|
-
# Free
|
133
|
-
# point them somewhere sensible
|
134
|
-
# STDOUT/STDERR should go to a logfile
|
135
|
-
|
32
|
+
# Free STDIN and point them somewhere sensible
|
136
33
|
begin; STDIN.reopen "/dev/null"; rescue ::Exception; end
|
137
34
|
end
|
138
35
|
module_function :simulate
|
139
36
|
|
140
37
|
|
38
|
+
# Call a given block as a daemon
|
141
39
|
def call_as_daemon(block, logfile_name = nil, app_name = nil)
|
40
|
+
# we use a pipe to return the PID of the daemon
|
142
41
|
rd, wr = IO.pipe
|
143
42
|
|
144
43
|
if tmppid = safefork
|
145
|
-
# parent
|
44
|
+
# in the parent
|
45
|
+
|
146
46
|
wr.close
|
147
47
|
pid = rd.read.to_i
|
148
48
|
rd.close
|
@@ -151,7 +51,7 @@ module Daemonize
|
|
151
51
|
|
152
52
|
return pid
|
153
53
|
else
|
154
|
-
# child
|
54
|
+
# in the child
|
155
55
|
|
156
56
|
rd.close
|
157
57
|
|
@@ -161,40 +61,19 @@ module Daemonize
|
|
161
61
|
end
|
162
62
|
|
163
63
|
# Prevent the possibility of acquiring a controlling terminal
|
164
|
-
|
165
|
-
|
166
|
-
exit if pid = safefork
|
167
|
-
#end
|
64
|
+
trap 'SIGHUP', 'IGNORE'
|
65
|
+
exit if pid = safefork
|
168
66
|
|
169
67
|
wr.write Process.pid
|
170
68
|
wr.close
|
171
69
|
|
172
|
-
# Always handle INT signal (thanks to Vit Ondruch)
|
173
|
-
#trap 'INT', 'DEFAULT' # make sure we always handle the 'INT' signal
|
174
|
-
|
175
70
|
$0 = app_name if app_name
|
176
71
|
|
177
|
-
|
72
|
+
# Release old working directory
|
73
|
+
Dir.chdir "/"
|
178
74
|
|
179
|
-
|
180
|
-
ObjectSpace.each_object(IO) do |io|
|
181
|
-
unless [STDIN, STDOUT, STDERR].include?(io)
|
182
|
-
begin
|
183
|
-
unless io.closed?
|
184
|
-
io.close
|
185
|
-
end
|
186
|
-
rescue ::Exception
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
ios = Array.new(8192){|i| IO.for_fd(i) rescue nil}.compact
|
192
|
-
ios.each do |io|
|
193
|
-
next if io.fileno < 3
|
194
|
-
io.close
|
195
|
-
end
|
75
|
+
close_io()
|
196
76
|
|
197
|
-
|
198
77
|
redirect_io(logfile_name)
|
199
78
|
|
200
79
|
block.call
|
@@ -205,10 +84,13 @@ module Daemonize
|
|
205
84
|
module_function :call_as_daemon
|
206
85
|
|
207
86
|
|
208
|
-
#
|
87
|
+
# Transform the current process into a daemon
|
209
88
|
def daemonize(logfile_name = nil, app_name = nil)
|
210
|
-
|
211
|
-
|
89
|
+
# Split rand streams between spawning and daemonized process
|
90
|
+
srand
|
91
|
+
|
92
|
+
# Fork and exit from the parent
|
93
|
+
safefork and exit
|
212
94
|
|
213
95
|
# Detach from the controlling terminal
|
214
96
|
unless sess_id = Process.setsid
|
@@ -216,19 +98,26 @@ module Daemonize
|
|
216
98
|
end
|
217
99
|
|
218
100
|
# Prevent the possibility of acquiring a controlling terminal
|
219
|
-
|
220
|
-
|
221
|
-
exit if pid = safefork
|
222
|
-
#end
|
223
|
-
|
224
|
-
# Always handle INT signal (thanks to Vit Ondruch)
|
225
|
-
#trap 'INT', 'DEFAULT' # make sure we always handle the 'INT' signal
|
101
|
+
trap 'SIGHUP', 'IGNORE'
|
102
|
+
exit if pid = safefork
|
226
103
|
|
227
104
|
$0 = app_name if app_name
|
228
105
|
|
229
|
-
|
106
|
+
# Release old working directory
|
107
|
+
Dir.chdir "/"
|
108
|
+
|
109
|
+
close_io()
|
230
110
|
|
231
|
-
|
111
|
+
redirect_io(logfile_name)
|
112
|
+
|
113
|
+
return sess_id
|
114
|
+
end
|
115
|
+
module_function :daemonize
|
116
|
+
|
117
|
+
|
118
|
+
def close_io()
|
119
|
+
# Make sure all input/output streams are closed
|
120
|
+
# Part I: close all IO objects (except for STDIN/STDOUT/STDERR)
|
232
121
|
ObjectSpace.each_object(IO) do |io|
|
233
122
|
unless [STDIN, STDOUT, STDERR].include?(io)
|
234
123
|
begin
|
@@ -239,18 +128,20 @@ module Daemonize
|
|
239
128
|
end
|
240
129
|
end
|
241
130
|
end
|
242
|
-
|
243
|
-
redirect_io(logfile_name)
|
244
131
|
|
245
|
-
#
|
246
|
-
|
132
|
+
# Make sure all input/output streams are closed
|
133
|
+
# Part II: close all file decriptors (except for STDIN/STDOUT/STDERR)
|
134
|
+
ios = Array.new(8192) {|i| IO.for_fd(i) rescue nil}.compact
|
135
|
+
ios.each do |io|
|
136
|
+
next if io.fileno < 3
|
137
|
+
io.close
|
138
|
+
end
|
247
139
|
end
|
248
|
-
module_function :
|
140
|
+
module_function :close_io
|
249
141
|
|
250
142
|
|
251
|
-
# Free file descriptors and
|
143
|
+
# Free STDIN/STDOUT/STDERR file descriptors and
|
252
144
|
# point them somewhere sensible
|
253
|
-
# STDOUT/STDERR should go to a logfile
|
254
145
|
def redirect_io(logfile_name)
|
255
146
|
begin; STDIN.reopen "/dev/null"; rescue ::Exception; end
|
256
147
|
|
@@ -271,4 +162,5 @@ module Daemonize
|
|
271
162
|
end
|
272
163
|
module_function :redirect_io
|
273
164
|
|
165
|
+
|
274
166
|
end
|
data/lib/daemons/pid.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 8
|
9
|
+
version: 1.1.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Thomas Uehlinger
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2012-02-07 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -61,10 +61,12 @@ files:
|
|
61
61
|
- examples/run/ctrl_proc.rb
|
62
62
|
- examples/run/ctrl_proc_multiple.rb
|
63
63
|
- examples/run/ctrl_proc_simple.rb
|
64
|
+
- examples/run/ctrl_slowstop.rb
|
64
65
|
- examples/run/myserver.rb
|
65
66
|
- examples/run/myserver_crashing.rb
|
66
67
|
- examples/run/myserver_exiting.rb
|
67
68
|
- examples/run/myserver_hanging.rb
|
69
|
+
- examples/run/myserver_slowstop.rb
|
68
70
|
- examples/call/call.rb
|
69
71
|
- examples/call/call_monitor.rb
|
70
72
|
- examples/daemonize/daemonize.rb
|