Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (68 sloc) 3.34 KB


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


Installing npm (node package manager)

  curl | sh

Installing daemon.node with npm

  [sudo] npm install daemon

Installing daemon.node locally

  node-waf configure build  


There is a great getting started article on daemons and node.js by Slashed that you can read here. The API has changed slightly from that version thanks to contributions from ptge and fugue; there is no longer a daemon.closeIO() method, this is done automatically for you.

Starting a daemon:

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

  var daemon = require('daemon');
  // Your awesome code here'somefile.log', 'w+', function (err, fd) {

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

  var sys = require('sys'),
      daemon = require('daemon');
  // Your awesome code here
  daemon.daemonize('somefile.log', '/tmp/', function (err, pid) {
    // We are now in the daemon process
    if (err) return sys.puts('Error starting daemon: ' + err);
    sys.puts('Daemon started successfully with pid: ' + pid);

Methods Available

daemon.start([fd for stdout and stderr])

If you supply a file descriptor it will redirect stdout and stderr to it, else stdout and stderr will be sent to /dev/null.


Closes stdin and reopens fd as /dev/null.


Closes stdout and reopens fd as /dev/null.


Closes stderr and reopens fd as /dev/null.


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


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.


Starts a new session for the process. Returns the SID as an integer.


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


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 created by Slashed and forked / improved / hacked upon by a lot of good people. Special thanks to Isaacs for npm and a great example in glob.

Author: Slashed

Contributors: Charlie Robbins, Pedro Teixeira, James Halliday, Zak Taylor, Daniel Bartlett