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(){
    log.debug('exit');
});

process.on('SIGINT', function(){
    log.debug('SIGINT');
});

process.on('uncaughtException', function(){
    log.debug('uncaughtException');
});

      

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

+3


source to share


1 answer


SIGINT

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
});

      

+5


source







All Articles