Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Turn your node daemon into an LSB-compatible init script.
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Turn your node daemon into an LSB-compatible init script.

For the impatient

init = require('init');

    pidfile : '/var/run/',
    logfile : '/var/log/myprog.log',
    command : process.argv[3],
    run     : function () {

init.simple() doesn't do what I want

You're in luck (maybe). simple() just makes the easy case easy. See the api methods below for more flexible ways to use this module.



Starts your service. This function will not return, and takes the following keyword arguments:


Required. This should be a path to a file to lock and store the daemon pid in. If the daemon is already running according to this pidfile, start succeeds without doing anything.


Path to a file to redirect your daemon's stdout and stderr to. Defaults to /dev/null.


Required. A function to be called after daemon setup is complete. Do your daemon work here.

success (pid, wasRunning)

A function to be called when the start action succeeded (already running or about to daemonize). 'pid' will be the id of the running process, and 'wasRunning' is true if the process was already running.


A function to be called if the start action cannot be performed. Error will be some sort of stringifiable error object. Defaults to init.startFailed.

init.stop(pidfile, cb, killer)

If you omit killer, it sends your service TERM, INT, QUIT, in that order (with 2 second delays) and then KILL until the process is no longer running, then calls cb (defaults to init.stopped). If the process was running, cb's first argument will be true. It is identical to pass init.hardKiller(2000) as killer argument. If you want to send SIGTERM signals to your process and wait until it die, use init.softKiller(delay = 2000) to check for your pid every delay seconds while it's alive. Then cb will be called.

init.status(pidfile, cb)

Gets the status of your service. The status is not returned, but rather will be passed to cb if you provide it (defaults to init.printStatus). It is an object of the form: { running: true, pid: 3472, exists: true }.


Higher level method that leaves all the callbacks as defaults and dispatches to calling the right function depending on the string you provide. Takes the following keyword arguments:




As in init.start()


As in init.stop()


A string on which to dispatch. Defaults to your program's first argument (process.argv[2]). Recognized actions are "start", "stop", "restart", "try-restart", "force-reload", and "status".

Default Actions

These functions are the defaults for various callbacks, but you can call them from your own custom callbacks if you want to augment them instead of replacing them.

init.startSucceeded(pid, wasRunning)

Prints "Started with PID n" or "Already running with PID n" and exits with a 0 status code.


Prints error and exits with a 1 status code.


Prints "Stopped" or "Not running" and exits with a 0 status code.

init.printStatus (status)

Prints a human-readable message and exits with an LSB-appropriate error code.

Program is running:

Process is already running with pid N.
exit 0

Program is dead (exited without removing pid file)

Pidfile exists, but process is dead.
exit 2

Program is not running:

Not running.
exit 3
Something went wrong with that request. Please try again.