Is it possible to catch when the app is being closed or getting sleep?

I am building some kind of little database for my real time application. After 12 hours it closes and reopens because I send http request every minute to keep app awake. In order not to lose the data I want to catch event when App closes and save data to other database. I tried:
process.on(‘SIGHUP’, function() {
console.log(‘About to exit’);
process.exit();
});

but it doesn’t seem to work.
Is it possible to catch when the app is being closed or getting sleep?

Hi @rromikas, welcome to the Glitch forums! I’ve never tried it myself, but my understanding is that you can listen for the SIGTERM event. There’s a little more detail on it here:

It works. Thank you. So to make it clear for others, I put this code to my project:
process.on(‘SIGTERM’, function() {
console.log(‘About to exit’);
process.exit(0);
});

2 Likes