pm2-exit-hook

Run some code when the process exits
The process.on('exit')
event doesn't catch all the ways a process can exit. This module catches:
- process SIGINT, SIGTERM and SIGHUP signals
- process beforeExit and exit events
- PM2 clustering process shutdown message (PM2 graceful reload)
Useful for cleaning up. You can also include async handlers.
Forked from exit-hook Supports pm2 cluster mode.
Install
$ npm install --save pm2-exit-hook
Usage
const exitHook = require('exit-hook');
exitHook(() => {
console.log('exiting');
});
exitHook(() => {
console.log('exiting 2');
});
exitHook(() => {
console.log('exiting 2');
});
exitHook(callback => {
setTimeout(() => {
console.log('exiting 3');
callback();
}, 1000);
});
throw new Error('unicorns');
License
MIT © Sindre Sorhus