I didn't see a correct response on this. The answer is at the bottom of this page in the docs. here
Run pm2 with this flag
pm2 start app.js --shutdown-with-message
and add a check for 'message' instead of or aswell as SIGINT
process.on('message', async (msg) => {
if(msg === 'shutdown'){
console.log( 'Shutting down...');
await shutdownHandler();
console.log('Exiting now...');
setTimeout(function() {
process.exit(0)
}, 1500)
}
})
This works on windows, not so sure why the timeout is there just yet I included it because the docs do but my function is async so im thinking the timeout is useless, does anyone else this it is useless in the above scenario ? maybe it just gives the app time to process.exit gracefully.
now
pm2 stop app.js
will run the shutdown procedure, to check logs (check shutdown has ran)
pm2 logs app.js
Hope this helps.