Node.js pm2 on exit

I am using pm2 to run a node app. I have to save the data before the application is closed. This code works fine in a shell:

process.on('exit', function(){

process.on('SIGINT', function(){

process.on('uncaughtException', function(){


When I stop the application using "pm2 stop" the code doesn't work. I think pm2 is killing the process.


source to share

1 answer


usually starts after user shutdown (eg Ctrl + C). Assuming what pm2

is causing the abrupt shutdown then SIGINT

will not start.

Instead, you should listen for a completion signal SIGTERM

, which should cover both scenarios

process.on('SIGTERM', function() {
    // clean up




All Articles