npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-daemon

v0.1.7

Published

Turn your node daemon into an LSB-like init script

Downloads

17

Readme

Init

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

Fork

Forked from: https://github.com/frodwith/node-init

Add synchronous method & fix a logging problem.

As documentation for daemon.node says : 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.

For the impatient

init = require('init');

init.simple({
    pidfile : '/var/run/myprog.pid',
    logfile : '/var/log/myprog.log',
    command : process.argv[3],
    run     : function () {
        doWhateverMyDaemonDoes();
    }
})

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.

API

init.start(options)

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

pidfile

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.

logfile

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

run

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.

failure(error)

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)

Stops your service with one of shutdown functions. Default is init.hardKiller(2000), but you may pass your own.

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 }.

init.simple(options)

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:

pidfile

run

logfile

As in init.start()

killer

As in init.stop()

command

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".

killer

As in init.stop()

Shutdown functions

init.hardKiller(delay = 2000)

Sends your service TERM, INT, QUIT, in that order (with 2000 ms 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. This is the default shutdown function.

init.softKiller(delay = 2000)

Sends your service TERM and waits until it dies with 2000 ms delays. If it is more important that your service shutdown gracefully (to preserve data integrity, etc) than that it exits promptly, this is a good choice.

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.

init.startFailed(error)

Prints error and exits with a 1 status code.

init.stopped(killed)

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