Do some event on app shutdown


#1

I’m wondering if it’s possible to register a callback to be run whenever my app is “restarted”, whether that’s just naturally closing, exiting on error, or through the refresh command. Often you see examples in node online that look like

function callbackAction() {
   //blah
};
process.on('SIGINT',callbackAction);

But similar code on my app has only worked when my app crashes!


#2

SIGINT catches the Ctrl C event, so I would suggest using process.on('exit', handleExit.bind(null)) instead.


#3

Right. Is the binding of a handleExit function necessary? I’ve passed a function to process.on(‘exit’) which saves some parameters to a file, but the parameters don’t seem to be written as I would expect.


#4

Are you writing the file synchronously (i.e. using I/O methods that end with Sync)? If you don’t, the write might not finish since the process will exit right after the handler finishes.