pax_global_header00006660000000000000000000000064121266256310014516gustar00rootroot0000000000000052 comment=f41e0ed93c8278b03ddb8e2af66b16bc29adfa6e daemon.node-1.1.0/000077500000000000000000000000001212662563100137045ustar00rootroot00000000000000daemon.node-1.1.0/.gitignore000066400000000000000000000001431212662563100156720ustar00rootroot00000000000000.lock-wscript build/ build/* *.swp *.node node_modules npm-debug.log fixtures/* fixtures/!.gitkeep daemon.node-1.1.0/.travis.yml000066400000000000000000000000641212662563100160150ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" daemon.node-1.1.0/LICENSE000066400000000000000000000021401212662563100147060ustar00rootroot00000000000000Copyright (c) 2009 Arthur (Slashed), Pedro Teixeira, James Halliday, Zak Taylor, Charlie Robbins 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.daemon.node-1.1.0/README.md000066400000000000000000000035311212662563100151650ustar00rootroot00000000000000# daemon [![Build Status](https://secure.travis-ci.org/indexzero/daemon.node.png)](http://travis-ci.org/indexzero/daemon.node) Turn a node script into a daemon. ## install via npm ``` npm install daemon ``` Requires node >= 0.8 ## examples ```javascript // this code is run twice // see implementation notes below console.log(process.pid); // after this point, we are a daemon require('daemon')(); // different pid because we are now forked // original parent has exited console.log(process.pid); ``` ## api ### daemon() Respawn the process (self) as a daemon. The parent process will exit at the point of this call. ### daemon.daemon(script, args, opt) Spawn the `script` with given `args` array as a daemonized process. Return the `child` process object. opt can optionally contain the following arguments: * stdout (file descriptor for stdout of the daemon) * stderr (file descriptor for stderr of the daemon) * env (environment for the daemon) (default: process.env) * cwd (current working directory for daemonized script) (default: process.cwd) ## implementation notes Daemon actually re-spawns the current application and runs it again. The only difference between the original and the fork is that the original will not execute past the `daemon()` call whereas the fork will. ## node versions prior to 0.8 Using this module on older versions of node (or older versions of this module) are not recommended due to how node works internally and the issues it can cause for daemons. ## Contributors [Charlie Robbins](http://nodejitsu.com) [Pedro Teixeira](https://github.com/pgte) [James Halliday](https://github.com/substack) [Zak Taylor](https://github.com/dobl) [Daniel Bartlett](https://github.com/danbuk) [Charlie McConnell](https://github.com/AvianFlu) [Slashed](http://github.com/slashed) [Roman Shtylman](http://github.com/shtylman) daemon.node-1.1.0/examples/000077500000000000000000000000001212662563100155225ustar00rootroot00000000000000daemon.node-1.1.0/examples/cluster.js000066400000000000000000000012251212662563100175410ustar00rootroot00000000000000var cluster = require('cluster'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; ++i) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); cluster.fork(); }); // daemonize after setting up cluster return require('../')(); } var http = require('http'); http.createServer(function(req, res) { res.writeHead(200); res.end('process: ' + process.pid); // just a demo to cycle workers // DO NOT DO THIS IN PRODUCTION process.exit(); }).listen(8000); daemon.node-1.1.0/index.js000066400000000000000000000024431212662563100153540ustar00rootroot00000000000000var child_process = require('child_process'); // daemonize ourselves module.exports = function(opt) { // we are a daemon, don't daemonize again if (process.env.__daemon) { return process.pid; } var args = [].concat(process.argv); // shift off node args.shift(); // our script name var script = args.shift(); opt = opt || {}; var env = opt.env || process.env; // the child process will have this set so we can identify it as being daemonized env.__daemon = true; // start ourselves as a daemon module.exports.daemon(script, args, opt); // parent is done return process.exit(); }; // daemonizes the script and returns the child process object module.exports.daemon = function(script, args, opt) { opt = opt || {}; var stdout = opt.stdout || 'ignore'; var stderr = opt.stderr || 'ignore'; var env = opt.env || process.env; var cwd = opt.cwd || process.cwd; var cp_opt = { stdio: ['ignore', stdout, stderr], env: env, cwd: cwd, detached: true }; // spawn the child using the same node process as ours var child = child_process.spawn(process.execPath, [script].concat(args), cp_opt); // required so the parent can exit child.unref(); return child; }; daemon.node-1.1.0/package.json000066400000000000000000000021521212662563100161720ustar00rootroot00000000000000{ "name": "daemon", "version": "1.1.0", "description": "Add-on for creating *nix daemons", "author": "Roman Shtylman ", "contributors": [ { "name": "Pedro Teixeira", "email": "pedro.teixeira@gmail.com" }, { "name": "Charlie Robbins", "email": "charlie.robbins@gmail.com" }, { "name": "James Halliday", "email": "mail@substack.net" }, { "name": "Zak Taylor", "email": "zak@dobl.com" }, { "name": "Daniel Bartlett", "email": "dan@f-box.org" }, { "name": "Charlie McConnell", "email": "charlie@charlieistheman.com" }, { "name": "Josh Holbrook", "email": "josh@nodejitsu.com" }, { "name": "Arthur (Slashed)", "email": "arthur@norgic.com" } ], "repository": { "type": "git", "url": "http://github.com/indexzero/daemon.node.git" }, "scripts": { "test": "mocha --ui qunit test/*.js" }, "devDependencies": { "mocha": "1.8.1", "after": "0.6.0" }, "main": "./index.js", "engines": { "node": ">= 0.8.0" } } daemon.node-1.1.0/test/000077500000000000000000000000001212662563100146635ustar00rootroot00000000000000daemon.node-1.1.0/test/daemon-test.js000066400000000000000000000025171212662563100174460ustar00rootroot00000000000000var assert = require('assert'); var http = require('http'); var spawn = require('child_process').spawn; var after = require('after'); function launch(args) { var child = spawn(process.execPath, args); child.stdout.pipe(process.stdout, {end: false}); child.stderr.pipe(process.stderr, {end: false}); return child; }; // sanity check that a no daemon process exits test('no daemon', function(done) { var script = __dirname + '/fixtures/nodaemon.js'; var child = launch([script]); child.on('exit', function(code) { assert.equal(code, 0); done(); }); }); test('simple', function(done) { var script = __dirname + '/fixtures/simple.js'; done = after(2, done); var port = 12345; var child = launch([script, port]); child.stdout.pipe(process.stdout, {end: false}); child.stderr.pipe(process.stderr, {end: false}); // spawning child should exit child.on('exit', function(code) { assert.equal(code, 0); done(); }); // wait for http server to start up setTimeout(function() { var opt = { host: 'localhost', port: port }; http.get(opt, function(res) { res.setEncoding('utf8'); res.on('data', function(chunk) { process.kill(chunk, 'SIGTERM'); done(); }); }); }, 500); }); daemon.node-1.1.0/test/fixtures/000077500000000000000000000000001212662563100165345ustar00rootroot00000000000000daemon.node-1.1.0/test/fixtures/.gitkeep000066400000000000000000000000001212662563100201530ustar00rootroot00000000000000daemon.node-1.1.0/test/fixtures/nodaemon.js000066400000000000000000000000711212662563100206700ustar00rootroot00000000000000// will exit immediately var daemon = require('../../'); daemon.node-1.1.0/test/fixtures/simple.js000066400000000000000000000005671212662563100203730ustar00rootroot00000000000000var http = require('http'); var daemon = require('../../'); var port = process.argv[2]; daemon({ stdout: process.stdout, stderr: process.stderr }); var server = http.createServer(function(req, res) { res.end('' + process.pid); }); server.listen(port); // safety, kills process if test framework doesn't setTimeout(function() { process.exit(); }, 5000);