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