Package Exports
- respawn
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (respawn) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
respawn
Spawn a process and restart it if it crashes.
npm install respawn
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
// or -1 for infinite restarts
sleep:1000, // time to sleep between restarts,
kill:30000, // wait 30s before force killing after stopping
stdio: [...] // forward stdio options
})
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 monitormonitor.stop(cb)
Stops the monitor (kills the process if its running with SIGTERM)monitor.status
Get the current monitor status. Available values arerunning
,stopping
,stopped
,crashed
andsleeping
Events
monitor.on('start')
The monitor has startedmonitor.on('stop')
The monitor has fully stopped and the process is killedmonitor.on('crash')
The monitor has crashed (too many restarts or spawn error).monitor.on('sleep')
monitor is sleepingmonitor.on('spawn', process)
New child process has been spawnedmonitor.on('exit', code, signal)
child process has exitedmonitor.on('stdout', data)
child process stdout has emitted datamonitor.on('stderr', data)
child process stderr has emitted datamonitor.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