UNPKG

7.31 kBMarkdownView Raw
1About
2=======
3
4Node module for easy creation of daemons for Node 0.8.x
5
6For Node 0.6.x compatibility see daemonize https://github.com/niegowski/node-daemonize
7
8Just write your daemon as plain node.js application
9(like `/examples/simple/app.js`) and a simple controller with Daemonize
10(like `/examples/simple/ctrl.js`).
11
12
13Installation
14==============
15```
16$ npm install daemonize2
17```
18
19
20Example
21=========
22
23``` js
24var daemon = require("daemonize2").setup({
25 main: "app.js",
26 name: "sampleapp",
27 pidfile: "sampleapp.pid"
28});
29
30switch (process.argv[2]) {
31
32 case "start":
33 daemon.start();
34 break;
35
36 case "stop":
37 daemon.stop();
38 break;
39
40 default:
41 console.log("Usage: [start|stop]");
42}
43```
44
45For more examples see `examples` folder.
46
47Documentation
48===============
49
50Daemonize works like standard `require()` but loaded module is
51forked to work in background as a daemon.
52
53Keep in mind that `stdin`, `stdout` and `stderr` are redirected
54to `/dev/null` so any output from daemon won't display in console.
55You need to use file for logging (ie like `/examples/advanced/app.js`).
56
57Also any uncaught exception won't be displayed in the console,
58so `process.on("uncaughtException", ...)` should be used to
59redirect output to some log file.
60
61## daemonize.setup(options)
62Creates new `Daemon` instance. Supported `options`:
63
64* `main` - main application module file to run as daemon (required)
65* `name` - daemon name (default: basename of main)
66* `pidfile` - pidfile path (default: `/var/run/[name].pid`)
67* `user` - name or id of user (default: current)
68* `group` - name or id of group (default: current)
69* `silent` - disable printing info to console (default: `false`)
70* `stopTimeout` - interval of daemon killing retry (default: `2s`)
71* `args` - additional node runtime arguments, ie `--debug`
72* `argv` - argv for daemon (default: `process.argv.slice(2)`)
73
74All paths are resolved relative to file that uses "daemonize".
75
76All commandline arguments will be passed to the child process unless
77overriden with `argv` option.
78
79## Daemon
80Daemon control class. It references controlled daemon.
81
82### Event: "starting"
83`function() { }`
84
85Emitted when `start()` is called and if daemon is not already running.
86
87### Event: "started"
88`function(pid) { }`
89
90Emitted when daemon successfully started after calling `start()`.
91
92### Event: "running"
93`function(pid) { }`
94
95Emitted when `start()` is called and a daemon is already running.
96
97### Event: "stopping"
98`function() { }`
99
100Emitted when `stop()` or `kill()` is called and a daemon is running.
101
102### Event: "stopped"
103`function(pid) { }`
104
105Emitted when daemon was successfully stopped after calling `stop()`
106or `kill()`.
107
108### Event: "notrunning"
109`function() { }`
110
111Emitted when `stop()` or `kill()` is called and a deamon is not running.
112
113### Event: "error"
114`function(error) { }`
115
116Emitted when `start()` failed. `error` is instance of `Error`.
117`error.message` contains information what went wrong.
118
119### daemon.start([listener])
120Start daemon asynchronously. Emits `running` in case when daemon is
121already running and `starting` when daemon is not running. Then emits
122`started` when daemon is successfully started.
123
124Optional `listener` callback is once called on `running`, `started` or `error`
125event. The callback gets two arguments `(err, pid)`.
126
127Emits `error` in case of any problem during daemon startup.
128
129### daemon.stop([listener])
130Asynchronously stop daemon. Sends `SIGTERM` to daemon every 2s (or time
131set in options).
132
133Emits `notrunning` when daemon is not running, otherwise
134emits `stopping` and then `stopped` when daemon successfully stopped.
135
136Optional `listener` callback is once called on `notrunning`, `stopped` or
137`error` event. The callback gets two arguments `(err, pid)`.
138
139### daemon.kill([listener])
140Kill daemon asynchronously. Sends `SIGTERM` and after 2s `SIGKILL` to the
141child if needed. Repeats sending `SIGKILL` every 2s untill daemon
142stops (interval can be changed in options).
143
144Emits events same as `stop()`.
145
146Optional `listener` callback is same as `stop`.
147
148### daemon.status()
149Synchronously returns pid for running daemon or 0 when daemon is not running.
150
151### daemon.sendSignal(signal)
152Synchronously sends `signal` to daemon and returns pid of daemon or 0 when
153daemon is not running.
154
155
156Changelog
157===========
158
159Daemonize is maintained under the [Semantic Versioning]
160(https://github.com/niegowski/semver/blob/master/semver.md)
161guidelines.
162
163### 0.4.0 - Jun 05 2013
164 - added argv option
165
166### 0.4.0-rc.6 - Nov 28 2012
167 - args option to enable node arguments ie --debug
168 - fix for: Wrapper seems to eat one argument
169
170### 0.4.0-rc.5 - Aug 28 2012
171 - Wrapper is transparent now
172
173### 0.4.0-rc.4 - Aug 16 2012
174 - The callback for start, stop and kill handles errors
175
176### 0.4.0-rc.3 - Aug 14 2012
177 - Optional callback argument for start, stop and kill
178
179### 0.4.0-rc.2 - Jul 29 2012
180 - Passing command line arguments to child process
181
182### 0.4.0-rc.1 - Jul 29 2012
183 - Daemonize forked as Daemonize2 for Node 0.8.x compatibility
184 - Removed native module for setsid - using child_process.spawn detached
185 - Passing options via ipc instead of command line arguments
186 - Rethrowing wrapper exceptions via ipc
187
188### 0.3.2 - Jul 29 2012
189 - Daemonize is compatible only with Node 0.6.x
190
191### 0.3.1 - Apr 2 2012
192
193### 0.3.0 - Jan 29 2012
194 - Daemon emits Events instead of console.log()
195 - API change - events in place of callbacks
196
197### 0.2.2 - Jan 27 2012
198 - root priviledges no longer required
199 - changed error exit codes
200 - try to remove pidfile on daemon stop
201 - configurable timeouts for start monitoring and killing
202 - closing FD-s on daemon start
203 - better examples
204
205### 0.2.1 - Jan 26 2012
206 - fix for calling callback in stop/kill when process is not running
207
208### 0.2.0 - Jan 26 2012
209 - code refactor
210 - stop listening for uncaughtException
211 - logfile removed
212
213### 0.1.2 - Jan 25 2012
214 - fixed stdout, stderr replacement
215 - checking for daemon main module presence
216 - signals change (added custom signals)
217 - better log messages
218 - gracefull terminate in example app
219 - close logfile on process exit
220
221### 0.1.1 - Jan 24 2012
222 - print stacktrace for uncaughtException
223
224### 0.1.0 - Jan 24 2012
225 - First release
226
227
228License
229=========
230
231(The MIT License)
232
233Copyright (c) 2012 Kuba Niegowski
234
235Permission is hereby granted, free of charge, to any person obtaining a copy
236of this software and associated documentation files (the "Software"), to deal
237in the Software without restriction, including without limitation the rights
238to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
239copies of the Software, and to permit persons to whom the Software is
240furnished to do so, subject to the following conditions:
241
242The above copyright notice and this permission notice shall be included in
243all copies or substantial portions of the Software.
244
245THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
246IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
248AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
249LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
250OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
251THE SOFTWARE.