forked from ghazel/daemons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
223 lines (147 loc) · 6.17 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
84
85
86
87
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
= Daemons Version 1.0.11
(See Releases for release-specific information)
== What is Daemons?
Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server)
to be <i>run as a daemon</i> and to be <i>controlled by simple start/stop/restart commands</i>.
If you want, you can also use daemons to <i>run blocks of ruby code in a daemon process</i> and to control
these processes from the main application.
Besides this basic functionality, daemons offers many advanced features like <i>exception backtracing</i>
and logging (in case your ruby script crashes) and <i>monitoring</i> and automatic restarting of your processes
if they crash.
Daemons includes the <tt>daemonize.rb</tt> script written by <i>Travis Whitton</i> to do the daemonization
process.
== Basic Usage
You can use Daemons in four differet ways:
=== 1. Create wrapper scripts for your server scripts or applications
Layout: suppose you have your self-written server <tt>myserver.rb</tt>:
# this is myserver.rb
# it does nothing really useful at the moment
loop do
sleep(5)
end
To use <tt>myserver.rb</tt> in a production environment, you need to be able to
run <tt>myserver.rb</tt> in the _background_ (this means detach it from the console, fork it
in the background, release all directories and file descriptors).
Just create <tt>myserver_control.rb</tt> like this:
# this is myserver_control.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
Daemons.run('myserver.rb')
And use it like this from the console:
$ ruby myserver_control.rb start
(myserver.rb is now running in the background)
$ ruby myserver_control.rb restart
(...)
$ ruby myserver_control.rb stop
For testing purposes you can even run <tt>myserver.rb</tt> <i>without forking</i> in the background:
$ ruby myserver_control.rb run
An additional nice feature of Daemons is that you can pass <i>additional arguments</i> to the script that
should be daemonized by seperating them by two _hyphens_:
$ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument
=== 2. Create wrapper scripts that include your server procs
Layout: suppose you have some code you want to run in the background and control that background process
from a script:
# this is your code
# it does nothing really useful at the moment
loop do
sleep(5)
end
To run this code as a daemon create <tt>myproc_control.rb</tt> like this and include your code:
# this is myproc_control.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
Daemons.run_proc('myproc.rb') do
loop do
sleep(5)
end
end
And use it like this from the console:
$ ruby myproc_control.rb start
(myproc.rb is now running in the background)
$ ruby myproc_control.rb restart
(...)
$ ruby myproc_control.rb stop
For testing purposes you can even run <tt>myproc.rb</tt> <i>without forking</i> in the background:
$ ruby myproc_control.rb run
=== 3. Control a bunch of daemons from another application
Layout: you have an application <tt>my_app.rb</tt> that wants to run a bunch of
server tasks as daemon processes.
# this is my_app.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
task1 = Daemons.call(:multiple => true) do
# first server task
loop {
conn = accept_conn()
serve(conn)
}
end
task2 = Daemons.call do
# second server task
loop {
something_different()
}
end
# the parent process continues to run
# we can even control our tasks, for example stop them
task1.stop
task2.stop
exit
=== 4. Daemonize the currently running process
Layout: you have an application <tt>my_daemon.rb</tt> that wants to run as a daemon
(but without the ability to be controlled by daemons via start/stop commands)
# this is my_daemons.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
# Initialize the app while we're not a daemon
init()
# Become a daemon
Daemons.daemonize
# The server loop
loop {
conn = accept_conn()
serve(conn)
}
<b>For further documentation, refer to the module documentation of Daemons.</b>
== Download and Installation
*Download*: just go to http://rubyforge.org/projects/daemons/
Installation *with* RubyGems:
$ su
# gem install daemons
Installation *without* RubyGems:
$ tar xfz daemons-x.x.x.tar.gz
$ cd daemons-x.x.x
$ su
# ruby setup.rb
== Documentation
For further documentation, refer to the module documentation of Daemons (click on Daemons).
The RDoc documentation is also online at http://daemons.rubyforge.org
== Author
Written in 2005-2008 by Thomas Uehlinger <mailto:[email protected]>.
== License
Copyright (c) 2005-2008 Thomas Uehlinger
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
This license does not apply to daemonize.rb, which is was written by
Travis Whitton und published under the following license:
The Daemonize extension module is copywrited free software by Travis Whitton
<[email protected]>. You can redistribute it under the terms specified in
the COPYING file of the Ruby distribution.
== Feedback and other resources
At http://rubyforge.org/projects/daemons.