I'm using pm2 to running node application. I must save data before application will be closed. This code works fine in shell:
process.on('exit', function(){
log.debug('exit');
});
process.on('SIGINT', function(){
log.debug('SIGINT');
});
process.on('uncaughtException', function(){
log.debug('uncaughtException');
});
When I'm stopping the application using "pm2 stop" the code doesn't work. I think that pm2 kills process.
SIGINT is generally triggered after a user-invoked shutdown (e.g. Ctrl+C). Assuming pm2 is triggering an abrupt shutdown then SIGINT won't be triggered.
Instead, you should listen for the termination signal SIGTERM which should cover both scenarios
process.on('SIGTERM', function() {
// clean up
});