1.0.2 • Published 9 years ago

daemonic-objects v1.0.2

Weekly downloads
9
License
MIT
Repository
github
Last release
9 years ago

daemonic-objects

Installation

npm install daemonic-objects

Examples

// this code is run twice
// see implementation notes below
console.log(process.pid);

// after this point, we are a daemon
var daemonic = require('daemonic-objects')();
daemonic.becomeDaemon();

// different pid because we are now forked
// original parent has exited
console.log(process.pid);

How To Use

daemonic.becomeDaemon(opt)

Respawn self as a daemon. The parent process will exit at the point of this call. The child process will completely re-execute the program so everything above the point of this call is executed twice. opt parameter see below.

console.log(process.pid); // printed twice
var daemonic = require('daemonic-objects')();
daemonic.becomeDaemon();
console.log(process.pid); // new pid

daemonic.spawnDaemon(script, args, opt)

Spawn the script with given args array as a daemonized process. Return the child process object. This is the function that actually turns the process into a daemon.

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

This implementation of the original module uses object literal notation and the factory pattern. This means daemonic-object can be instantiated multiple times and all its methods and properties are public, enabling easier testing.

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.