1 |
3a515b92
|
cagy
|
# signal-exit
|
2 |
|
|
|
3 |
|
|
[](https://travis-ci.org/tapjs/signal-exit)
|
4 |
|
|
[](https://coveralls.io/r/tapjs/signal-exit?branch=master)
|
5 |
|
|
[](https://www.npmjs.com/package/signal-exit)
|
6 |
|
|
[](https://github.com/conventional-changelog/standard-version)
|
7 |
|
|
|
8 |
|
|
When you want to fire an event no matter how a process exits:
|
9 |
|
|
|
10 |
|
|
* reaching the end of execution.
|
11 |
|
|
* explicitly having `process.exit(code)` called.
|
12 |
|
|
* having `process.kill(pid, sig)` called.
|
13 |
|
|
* receiving a fatal signal from outside the process
|
14 |
|
|
|
15 |
|
|
Use `signal-exit`.
|
16 |
|
|
|
17 |
|
|
```js
|
18 |
|
|
var onExit = require('signal-exit')
|
19 |
|
|
|
20 |
|
|
onExit(function (code, signal) {
|
21 |
|
|
console.log('process exited!')
|
22 |
|
|
})
|
23 |
|
|
```
|
24 |
|
|
|
25 |
|
|
## API
|
26 |
|
|
|
27 |
|
|
`var remove = onExit(function (code, signal) {}, options)`
|
28 |
|
|
|
29 |
|
|
The return value of the function is a function that will remove the
|
30 |
|
|
handler.
|
31 |
|
|
|
32 |
|
|
Note that the function *only* fires for signals if the signal would
|
33 |
|
|
cause the proces to exit. That is, there are no other listeners, and
|
34 |
|
|
it is a fatal signal.
|
35 |
|
|
|
36 |
|
|
## Options
|
37 |
|
|
|
38 |
|
|
* `alwaysLast`: Run this handler after any other signal or exit
|
39 |
|
|
handlers. This causes `process.emit` to be monkeypatched.
|