Do some event on app shutdown

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() {

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

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

1 Like

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.

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.