0

I have a node js express app hosted on Azure (Microsoft server). For some reason, I get a feeling that the app restarts once in a while, and I want to catch and log this event. So I try this but it doesn't work:

process.on('exit', function (code) {
    var exitMsg = 'Process exited with code ' + code;

    // log exitMsg synchronously
});

I understand that on Windows there is some problem catching the exit event. Maybe you have some solution? Thanks.

Mister_L
  • 2,469
  • 6
  • 30
  • 64

2 Answers2

1

On azure/win this might help:

if (process.platform === "win32") {
  require("readline")
    .createInterface({
      input: process.stdin,
      output: process.stdout
    })
    .on("SIGINT", function () {
      process.emit("SIGINT");
    });
}
Tony Gutierrez
  • 753
  • 1
  • 6
  • 16
0

I think you may be looking for the 'SIGINT' event. It occurs when your app is interrupted or cancelled.

process.on('SIGINT', () => {
  console.log('Process Interrupted, exiting');

  // eventually exit
  process.exit(); // Add code if necessary
});

Its worth noting though, if you're hosting your App in IIS using IISNode, this probably won't work.

peteb
  • 18,552
  • 9
  • 50
  • 62
  • Yes there is, `SIGINT from the terminal is supported on all platforms, and can usually be generated with CTRL+C (though this may be configurable). It is not generated when terminal raw mode is enabled.` – peteb Mar 01 '17 at 16:40
  • @jfriend00 just tested in `cmd.exe` and `powershell`, validated it works in both environments on Windows 7 – peteb Mar 01 '17 at 16:46
  • 1
    OK, I guess it was added since [this post](http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js/14861513#14861513). – jfriend00 Mar 01 '17 at 16:56