# respawn

> Spawn a process and restart it if it crashes

Latest version **2.6.0** (published 2019-01-14) · 0 weekly downloads

## Install

```sh
npm install respawn
pnpm add respawn
yarn add respawn
bun add respawn
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.6.0 |
| Published | 2019-01-14 |
| First published | 2013-12-10 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 252 |
| Author | Mathias Buus |
| Maintainers | mafintosh |
| Keywords | spawn, restart, keep, alive, process, monitor, service, start, stop |

## Links

- npm: https://www.npmjs.com/package/respawn
- Repository: https://github.com/mafintosh/respawn
- Homepage: https://github.com/mafintosh/respawn#readme
- Issues: https://github.com/mafintosh/respawn/issues
- npm.io page: https://npm.io/package/respawn

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ^4.0.1
- [ps-tree](https://npm.io/package/ps-tree.md) ^1.2.0

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 2.6.0 (latest) — 2019-01-14
- 2.5.0 — 2017-02-16
- 2.4.1 — 2016-05-17
- 2.4.0 — 2016-05-10
- 2.3.0 — 2016-02-24
- 2.2.0 — 2016-01-30
- 2.1.0 — 2016-01-05
- 2.0.0 — 2015-05-18
- 1.1.0 — 2015-04-16
- 1.0.2 — 2015-03-10
- 1.0.1 — 2015-01-07
- 1.0.0 — 2014-11-24
- 0.4.2 — 2014-05-20
- 0.4.1 — 2014-05-13
- 0.4.0 — 2014-05-03
- … 12 more at https://npm.io/package/respawn/versions

## README

# respawn

Spawn a process and restart it if it crashes.

```
npm install respawn
```

[![Build Status](https://travis-ci.org/mafintosh/respawn.png)](https://travis-ci.org/mafintosh/respawn)

## Usage

It is easy to use

``` js
var respawn = require('respawn')

var monitor = respawn(['node', 'server.js'], {
  name: 'test',          // set monitor name
  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
  fork: true             // fork instead of spawn
})

monitor.start() // spawn and watch
```

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

Per default respawn will restart you app indefinitely. To set a max restart limit set the `maxRestarts` option.

If `sleep` is an array of numbers it will use the value at the position of the current number of restarts as the timeout value. If the number of restarts exceed the length of the array it will use the last value in the array until it hits the maxRestarts.

`sleep: [1000, 60000, 60000, 12000, 1000]` will wait 1000ms before retrying, then it will wait 60000 before the next retry and so forth.

If `sleep` is a function it will be passed the number of times (including this one) that the app has been restarted (i.e. first time will be called with 1, second time 2 etc.) and should return a time in milliseconds.

## 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`, `crashed` 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('crash')`  The monitor has crashed (too many restarts or spawn error).

* `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

``` js
// 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

---
_Source: https://npm.io/package/respawn · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
