Giter Club home page Giter Club logo

node-daemonize's Introduction

About

Node module for easy creation of daemons.

Just write your daemon as plain node.js application (like /examples/simple/app.js) and a simple controller with Daemonize (like /examples/simple/ctrl.js).

Installation

$ npm install daemonize

Example

var daemon = require("daemonize").setup({
    main: "app.js",
    name: "sampleapp",
    pidfile: "sampleapp.pid"
});

switch (process.argv[2]) {
    
    case "start": 
        daemon.start().once("started", function() {
            process.exit();
        });
        break;
    
    case "stop":
        daemon.stop();
        break;
    
    default:
        console.log("Usage: [start|stop]");
}

For more examples see examples folder.

Documentation

Daemonize works like standard require() but loaded module is forked to work in background as a daemon.

Keep in mind that stdin, stdout and stderr are redirected to /dev/null so any output from daemon won't display in console. You need to use file for logging (ie like /examples/advanced/app.js).

Also any uncaught exception won't be displayed in the console, so process.on("uncaughtException", ...) should be used to redirect output to some log file.

daemonize.setup(options)

Creates new Daemon instance. Supported options:

  • main - main application module file to run as daemon (required)
  • name - daemon name (default: basename of main)
  • pidfile - pidfile path (default: /var/run/[name].pid)
  • user - name or id of user (default: current)
  • group - name or id of group (default: current)
  • silent - disable printing info to console (default: false)
  • stopTimeout - interval of daemon killing retry (default: 2s)

All paths are resolved relative to file that uses "daemonize".

All other options will be passed to the child process as posix style arguments (--myarg1=abc).

Daemon

Daemon control class. It references controlled daemon.

Event: "starting"

function() { }

Emitted when start() is called and if daemon is not already running.

Event: "started"

function(pid) { }

Emitted when daemon successfully started after calling start().

Event: "running"

function(pid) { }

Emitted when start() is called and a daemon is already running.

Event: "stopping"

function() { }

Emitted when stop() or kill() is called and a daemon is running.

Event: "stopped"

function(pid) { }

Emitted when daemon was successfully stopped after calling stop() or kill().

Event: "notrunning"

function() { }

Emitted when stop() or kill() is called and a deamon is not running.

Event: "error"

function(error) { }

Emitted when start() failed. error is instance of Error. error.message contains information what went wrong.

daemon.start()

Start daemon asynchronously. Emits running in case when daemon is already running and starting when daemon is not running. Then emits started when daemon is successfully started.

Emits error in case of any problem during daemon startup.

daemon.stop()

Asynchronously stop daemon. Sends SIGTERM to daemon every 2s (or time set in options).

Emits notrunning when daemon is not running, otherwise emits stopping and then stopped when daemon successfully stopped.

daemon.kill()

Kill daemon asynchronously. Sends SIGTERM and after 2s SIGKILL to the child if needed. Repeats sending SIGKILL every 2s untill daemon stops (interval can be changed in options).

Emits events same as stop().

daemon.status()

Synchronously returns pid for running daemon or 0 when daemon is not running.

daemon.sendSignal(signal)

Synchronously sends signal to daemon and returns pid of daemon or 0 when daemon is not running.

Changelog

Daemonize is maintained under the [Semantic Versioning] (https://github.com/niegowski/semver/blob/master/semver.md) guidelines.

0.3.0 - Jan 29 2012

  • Daemon emits Events instead of console.log()
  • API change - events in place of callbacks

0.2.2 - Jan 27 2012

  • root priviledges no longer required
  • changed error exit codes
  • try to remove pidfile on daemon stop
  • configurable timeouts for start monitoring and killing
  • closing FD-s on daemon start
  • better examples

0.2.1 - Jan 26 2012

  • fix for calling callback in stop/kill when process is not running

0.2.0 - Jan 26 2012

  • code refactor
  • stop listening for uncaughtException
  • logfile removed

0.1.2 - Jan 25 2012

  • fixed stdout, stderr replacement
  • checking for daemon main module presence
  • signals change (added custom signals)
  • better log messages
  • gracefull terminate in example app
  • close logfile on process exit

0.1.1 - Jan 24 2012

  • print stacktrace for uncaughtException

0.1.0 - Jan 24 2012

  • First release

License

(The MIT License)

Copyright (c) 2012 Kuba Niegowski

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.