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

I follow the instructions here (listening for SIGINT events) to gracefully close my node.js application hosted on Windows 8 in response to Ctrl-C or shutting down the server.

But Windows does not have SIGINT . I also tried process.on('exit') , but this seems to be doing something productive lately.

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 it's too late to do something elegant :

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

Is there an equivalent SIGINT event on Windows?

+49
windows
Apr 05 2018-12-12T00:
source share
5 answers

You must use the readline module and listen for the 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(); }); 
+87
Feb 13 '13 at 19:32
source share

If you do not need the readline import for other tasks, I would suggest importing readline after the program has verified that it works on Windows. In addition, for those who may not know, this works on both 32-bit and Windows 64-bit systems (which will return the keyword "win32"). Thanks for this Gabriel solution.

 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(); }); 
+7
Sep 07 '13 at 20:44
source share

Currently, node support for capturing Windows console management events is still missing, so there are no POSIX signal equivalents:

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

However, the tty module documentation provides an example of a keystroke capture mechanism to trigger a graceful shutdown, but then it only works 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(); } }); 
+4
Apr 05 2018-12-12T00:
source share

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

enter image description here

also works with windows command line.

+3
Jul 28 '17 at 9:20
source share

Since node.js 0.8 the keypress event no longer exists. However, there is an npm package called keypress that repeats the event.

Install using 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"); } }); } 
0
Jan 01 '13 at 21:47
source share



All Articles