Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api] Rename start()/run() to daemonize(). Rename stop() to kill(). U…

…pdate tabs -> spaces.
  • Loading branch information...
commit b943c8b44385882e400c6b5f0877b97522a44504 1 parent 7d0c8f1
Charlie Robbins indexzero authored
2  README.md
View
@@ -43,7 +43,7 @@ This library also exposes a higher level facility through javascript for startin
// Your awesome code here
- daemon.run('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
+ daemon.daemonize('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
// We are now in the daemon process
if (err) return sys.puts('Error starting daemon: ' + err);
34 example/bindings.js
View
@@ -19,36 +19,36 @@ catch (ex) {
}
var config = {
- lockFile: '/tmp/testd.pid', // Location of lockFile
- logFile: '/tmp/testd.log' // Location of logFile
+ lockFile: '/tmp/testd.pid', // Location of lockFile
+ logFile: '/tmp/testd.log' // Location of logFile
};
var args = process.argv;
// Handle start stop commands
switch(args[2]) {
- case "stop":
- process.kill(parseInt(fs.readFileSync(config.lockFile)));
- process.exit(0);
- break;
-
- case "start":
+ case "stop":
+ process.kill(parseInt(fs.readFileSync(config.lockFile)));
+ process.exit(0);
+ break;
+
+ case "start":
fs.open(config.logFile, 'w+', function (err, fd) {
if (err) return sys.puts('Error starting daemon: ' + err);
daemon.start(fd);
- daemon.lock(config.lockFile);
+ daemon.lock(config.lockFile);
});
- break;
-
- default:
- sys.puts('Usage: [start|stop]');
- process.exit(0);
+ break;
+
+ default:
+ sys.puts('Usage: [start|stop]');
+ process.exit(0);
}
// Start HTTP Server
http.createServer(function(req, res) {
- res.writeHead(200, { 'Content-Type': 'text/html' });
- res.write('<h1>Hello, World!</h1>');
- res.end();
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.write('<h1>Hello, World!</h1>');
+ res.end();
}).listen(8000);
43 example/wrapper.js
View
@@ -19,38 +19,41 @@ catch (ex) {
}
var config = {
- lockFile: '/tmp/testd.pid', // Location of lockFile
- logFile: '/tmp/testd.log' // Location of logFile
+ lockFile: '/tmp/testd.pid', // Location of lockFile
+ logFile: '/tmp/testd.log' // Location of logFile
};
var args = process.argv;
// Handle start stop commands
switch(args[2]) {
- case "stop":
- daemon.stop(config.lockFile, function (err, pid) {
- if (err) return sys.puts('Error stopping daemon: ' + err);
- sys.puts('Successfully stopped daemon with pid: ' + pid);
- });
- break;
-
- case "start":
- // Start HTTP Server
+ case "stop":
+ daemon.kill(config.lockFile, function (err, pid) {
+ if (err) return sys.puts('Error stopping daemon: ' + err);
+ sys.puts('Successfully stopped daemon with pid: ' + pid);
+ });
+ break;
+
+ case "start":
+ // Start HTTP Server
http.createServer(function(req, res) {
// sys.puts('Incoming request for: ' + req.url);
- res.writeHead(200, { 'Content-Type': 'text/html' });
- res.write('<h1>Hello, World!</h1>');
- res.end();
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.write('<h1>Hello, World!</h1>');
+ res.end();
}).listen(8000);
- daemon.run(config.logFile, config.lockFile, function (err, started) {
- if (err) return sys.puts('Error starting daemon: ' + err);
+ daemon.daemonize(config.logFile, config.lockFile, function (err, started) {
+ if (err) {
+ console.dir(err.stack);
+ return sys.puts('Error starting daemon: ' + err);
+ }
sys.puts('Successfully started daemon');
});
break;
-
- default:
- sys.puts('Usage: [start|stop]');
- break;
+
+ default:
+ sys.puts('Usage: [start|stop]');
+ break;
}
8 lib/daemon.js
View
@@ -20,12 +20,12 @@ Object.keys(binding).forEach(function (k) { daemon[k] = binding[k] });
// Run is designed to encapsulate the basic daemon operation in a single async call.
// When the callback returns you are in the the child process.
//
-daemon.run = function (out, lock, callback) {
+daemon.daemonize = function (out, lock, callback) {
fs.open(out, 'w+', function (err, fd) {
if (err) return callback(err);
try {
- daemon.start(fd);
+ binding.daemonize(fd);
daemon.lock(lock);
callback(null, true);
}
@@ -36,11 +36,11 @@ daemon.run = function (out, lock, callback) {
};
//
-// function lock (lock, callback)
+// function kill (lock, callback)
// Asynchronously stop the process in the lock file and
// remove the lock file
//
-daemon.stop = function (lock, callback) {
+daemon.kill = function (lock, callback) {
fs.readFile(lock, function (err, data) {
if (err) return callback(err);
19 src/daemon.cc
View
@@ -30,9 +30,9 @@ using namespace node;
//
// Go through special routines to become a daemon.
-// if successful, returns daemon's PID
+// if successful, returns daemon pid
//
-Handle<Value> Start(const Arguments& args) {
+Handle<Value> Daemonize(const Arguments& args) {
HandleScope scope;
pid_t pid, sid;
int i, new_fd;
@@ -84,6 +84,15 @@ Handle<Value> CloseStdout(const Arguments& args) {
}
//
+// Closes all stdio by redirecting to /dev/null
+//
+Handle<Value> CloseStdio(const Arguments& args) {
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stderr);
+ freopen("/dev/null", "w", stdout);
+}
+
+//
// File-lock to make sure that only one instance of daemon is running, also for storing pid
// lock (filename)
// @filename: a path to a lock-file.
@@ -179,9 +188,13 @@ Handle<Value> SetReuid(const Arguments& args) {
extern "C" void init(Handle<Object> target) {
HandleScope scope;
- NODE_SET_METHOD(target, "start", Start);
+ NODE_SET_METHOD(target, "daemonize", Daemonize);
NODE_SET_METHOD(target, "lock", LockD);
NODE_SET_METHOD(target, "setsid", SetSid);
NODE_SET_METHOD(target, "chroot", Chroot);
NODE_SET_METHOD(target, "setreuid", SetReuid);
+ NODE_SET_METHOD(target, "closeStderr", CloseStderr);
+ NODE_SET_METHOD(target, "closeStdout", CloseStdout);
+ NODE_SET_METHOD(target, "cloudStdin", CloseStdin);
+ NODE_SET_METHOD(target, "closeStdio", CloseStdio);
}
Please sign in to comment.
Something went wrong with that request. Please try again.