What is the Windows equivalent of process.on('SIGINT') in node.js?

Go To StackoverFlow.com

67

I'm following the guidance here (listening for SIGINT events) to gracefully shutdown my Windows-8-hosted node.js application in response to Ctrl-C or server shutdown.

But Windows doesn't have SIGINT. I also tried process.on('exit'), but that seems to late to do anything productive.

On Windows, this code gives me: Error: No such module

process.on( 'SIGINT', function() {
  console.log( "\ngracefully shutting down from  SIGINT (Crtl-C)" )
  // wish this worked on Windows
  process.exit( )
})

On Windows, this code runs, but is too late to do anything graceful:

process.on( 'exit', function() {
  console.log( "never see this log message" )
})

Is there a SIGINT equivalent event on Windows?

2012-04-05 01:05
by pappadog
This problem randomly occured to me today and I think it has something to do with the readline module itself. I couldn't do any testing but since I added this module I started having the problem - Sv443 2019-02-01 20:47


121

You have to use the readline module and listen for a SIGINT event:

http://nodejs.org/api/readline.html#readline_event_sigint

if (process.platform === "win32") {
  var rl = require("readline").createInterface({
    input: process.stdin,
    output: process.stdout
  });

  rl.on("SIGINT", function () {
    process.emit("SIGINT");
  });
}

process.on("SIGINT", function () {
  //graceful shutdown
  process.exit();
});
2013-02-13 19:32
by Gabriel Llamas
most excellent update, thanks! setting this as the answer as the previous answer (listen for keypress) no longer works - pappadog 2013-02-16 00:50
Upvote a million times - yourdeveloperfriend 2013-04-08 20:39
you sir, Rock. https://github.com/bevacqua/node-sigin - bevacqua 2013-05-16 17:25
This is ridiculous. Why isn't this handled by the node core - balupton 2013-11-19 12:00
Because when you listen to stdin the process never finishes until you send a SIGINT signal explicitly - Gabriel Llamas 2013-12-05 09:19
How do you send a SIGINT to a child process on windows? My child.kill('SIGINT') does not seem to work even with readline - Nick Sotiros 2014-05-16 23:34
Windows does not have signals, you cannot send a SIGINT to a child process. The main process is who can listen the stdin and that's how you can mimic the SIGINT on Windows, but child processes can't - Gabriel Llamas 2014-05-18 14:54
Therefore, you need to send a random message from the parent to the child, for example: "SIGINT" - Gabriel Llamas 2014-05-18 19:28
@GabrielLlamas this can't intercept process.kill(pid, 'SIGINT') though, can it - Andy 2016-10-29 02:39
Just windows thing - Luca Steeb 2017-01-04 19:49
Thank you so much for this! This problem was driving me crazy - Derik Taylor 2017-03-16 23:44
Looks like this got solved quite a while ago: https://github.com/nodejs/node-v0.x-archive/issues/505 - SimonSimCity 2017-11-20 10:01


11

I'm not sure as of when, but on node 8.x and on Windows 10 the original question code simply works now.

enter image description here

also works with a windows command prompt.

2017-07-28 09:20
by Meirion Hughes
I noticed this as well. Windows 7 is a major issue for me her - Gregory Nowakowski 2017-08-29 19:51
This method works for me with Node 8.11.1 on Windows 7 - but I was running it from a git bash shell. Thought I'd try the simple way first and it worked - user944849 2018-04-27 16:29


7

Unless you need the "readline" import for other tasks, I would suggest importing "readline" once the program has verified that it's running on Windows. Additionally, for those who might be unaware - this works on both Windows 32-bit and Windows 64-bit systems (which will return the keyword "win32"). Thanks for this solution Gabriel.

if (process.platform === "win32") {
  require("readline")
    .createInterface({
      input: process.stdin,
      output: process.stdout
    })
    .on("SIGINT", function () {
      process.emit("SIGINT");
    });
}

process.on("SIGINT", function () {
  // graceful shutdown
  process.exit();
});
2013-09-07 20:44
by tfmontague


4

Currently there is still no support in node for capturing the windows console control events, so there are no equivalents to the POSIX signals:

https://github.com/joyent/node/issues/1553

However the tty module documentation does give an example of a mechanism to capture the key presses in order to initiate a graceful shutdown, but then this does only work for ctrl+c.

var tty = require('tty');

process.stdin.resume();
tty.setRawMode(true);

process.stdin.on('keypress', function(char, key) {
  if (key && key.ctrl && key.name == 'c') {
    console.log('graceful exit of process %d', process.pid);
    process.exit();
  }
});
2012-04-05 05:56
by Pero P.
Thanks, been looking for this info, this is an acceptable substitution for me as long as I can implement it on the server for for CTRL+C. +1 (But.. Any idea whether this will effect performance by adding event listeners on the process? - Cory Gross 2012-07-28 22:19
I tried doing this, but when my server is running the game loop stdin does not seem to be available, CTRL+C doesn't work when I use the above - Cory Gross 2012-07-28 23:37


3

Nowadays it just works on all platforms, including Windows.

The following code logs and then terminates properly on Windows 10:

process.on('SIGINT', () => {
    console.log("Terminating...");
    process.exit(0);
});
2018-01-19 09:13
by Heinrich Ulbricht
This does NOT work for me under cygwin. Using latest Windows 10 (automatic updates), node version 8.11.4. By "it does NOT work" I mean that 1) the process does terminate, but 2) the message is not logged to the console and 3) HTTP connections created are not shut down. I should add, however, that I tried it under PowerShell, and it works as expected there. But I switched to cygwin because PowerShell has a bad curl command. Darn - user1738579 2018-09-10 03:15
It stopped working for me just today for no apparent reason (no updates at all) and I still don't know why. The accepted answer fixed it - Sv443 2019-02-01 20:44


0

Since node.js 0.8 the keypress event no longer exists. There is however an npm package called keypress that reimplements the event.

Install with npm install keypress, then do something like:

// Windows doesn't use POSIX signals
if (process.platform === "win32") {
    const keypress = require("keypress");
    keypress(process.stdin);
    process.stdin.resume();
    process.stdin.setRawMode(true);
    process.stdin.setEncoding("utf8");
    process.stdin.on("keypress", function(char, key) {
        if (key && key.ctrl && key.name == "c") {
            // Behave like a SIGUSR2
            process.emit("SIGUSR2");
        } else if (key && key.ctrl && key.name == "r") {
            // Behave like a SIGHUP
            process.emit("SIGHUP");
        }
    });
}
2013-01-01 21:47
by mekwall
Ads