Skip to content

manobi/daemon.node

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

daemon.node

This library should be considered DEPRECATED. You should be using detached child processes in Node.js core instead!

A C++ add-on for Node.js to enable simple daemons in Javascript plus some useful wrappers in Javascript.

Installation

Installing daemon.node with npm

  [sudo] npm install daemon

Installing daemon.node locally

  node-waf configure build  

Usage

Caveats Regarding Forking Safety

As of v0.6, node.js has not been fork-safe. What this means for you is that all daemonization should happen on the first tick and not as part of an asynchronous action. The easiest way to ensure this is to daemonize your process very early in the script, near the "require" block.

daemon.kill, however, is still asynchronous.

Starting a daemon:

Starting a daemon is easy, just call daemon.start() and daemon.lock().

var daemon = require('daemon'),
    pid;

pid = daemon.start('stdout.log', 'stderr.log');
daemon.lock('/tmp/yourprogram.pid');

daemon.start daemonizes your script's process and redirects stdio to the specified files. daemon.lock places a lockfile on your daemon.

This library also exposes a higher level facility through javascript for starting daemons:

  var daemon = require('daemon'),
      pid;
  
  pid = daemon.daemonize({ stdout: 'somefile.log', stderr: 'error.log' }, '/tmp/yourprogram.pid');
  console.log('Daemon started successfully with pid: ' + pid);

If you wish you can also simply pass a single pass which you wish to be used for both stdout and stderr:

  var daemon = require('daemon'),
      pid;
  
  pid = daemon.daemonize('stdout-and-stderr.log', '/tmp/yourprogram.pid');
  console.log('Daemon started successfully with pid: ' + pid);

Methods

daemon.start(stdout[, stderr])

Takes two filenames, one for stdout and one for stderr. If only stdout is supplied, stderr will use the same filename. If no arguments are passed, stdout and stderr output will be sent to /dev/null. Returns the process pid.

daemon.lock('/tmp/lockfile.pid')

Try to lock the file. If it's unable to OPEN the file it will exit. If it's unable to get a LOCK on the file it will return false. Else it will return true.

daemon.daemonize({ stdout: 'stdout.log', stderr: 'stderr.log' }, '/tmp/lockfile.pid', [cb])

A convenience wrapper around daemon.start and daemon.lock. Returns pid, optionally calls cb(err, pid) for error handling and backwards compatibility. This method is still synchronous.

daemon.kill(lockfile, cb)

Kills the process specified in the lockfile and cleans the file. Unlike every other method in this library, this one is asynchronous.

daemon.closeStdin()

Closes stdin and reopens fd as /dev/null.

daemon.closeStdout()

Closes stdout and reopens fd as /dev/null.

daemon.closeStderr()

Closes stderr and reopens fd as /dev/null.

daemon.closeStdio()

Closes std[in|out|err] and reopens fd as /dev/null.

daemon.chroot('/path_to_chroot_to')

Attempts to chroot the process, returns exception on error, returns true on success.

daemon.setreuid(1000)

Change the effective user of the process. Can take either an integer (UID) or a string (Username). Returns exceptions on error and true on success.

The Fine Print

This library is available under the MIT LICENSE. See the LICENSE file for more details. It was originally created by Slashed and has been forked/improved/hacked upon by a lot of good people. Special thanks to Isaacs for npm and a great example in glob.

Author: Slashed

About

A node.JS addon that helps to create *nix daemons in Javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 52.4%
  • C++ 43.0%
  • Python 2.8%
  • Shell 1.8%