Coder Social home page Coder Social logo

respawn's Introduction

respawn

Spawn a process and restart it if it crashes.

npm install respawn

Build Status

Usage

It is easy to use

var respawn = require('respawn');

var monitor = respawn(['node', 'server.js'], {
	env: {ENV_VAR:'test'}, // set env vars
	cwd: '.',              // set cwd
	maxRestarts:10,        // how many restarts are allowed within 60s
	sleep:1000,            // time to sleep between restarts
});

monitor.start(); // spawn and watch

Optionally you can specify the command to to spawn in the option map as command: [...]

API

  • monitor.start() Starts the monitor

  • monitor.stop(cb) Stops the monitor (kills the process if its running with SIGTERM)

  • monitor.status Get the current monitor status. Available values are running, stopping, stopped and sleeping

Events

  • monitor.on('start') The monitor has started

  • monitor.on('stop') The monitor has fully stopped and the process is killed

  • monitor.on('sleep') monitor is sleeping

  • monitor.on('spawn', process) New child process has been spawned

  • monitor.on('exit', code, signal) child process has exited

  • monitor.on('stdout', data) child process stdout has emitted data

  • monitor.on('stderr', data) child process stderr has emitted data

  • monitor.on('warn', err) child process has emitted an error

Graceful restart

To do graceful restart simply have your app stop gracefully when receiving SIGTERM and do

// graceful restart (do not wait for old process to die)
monitor.stop();
monitor.start();

// hard restart (wait for old process to die)
monitor.stop(function() {
	monitor.start();
});

License

MIT

respawn's People

Contributors

mafintosh avatar

Stargazers

Amit Kosti avatar Anthony Shauchenka avatar

Watchers

Alexander Beletsky avatar James Cloos avatar Dmytro Voronianski avatar  avatar

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.