Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 879
Q 4 Avatar asked Apr 05 '12 01:04

Q 4


People also ask

What is Sigint in node JS?

SIGINT is generated by the user pressing Ctrl + C and is an interrupt. SIGTERM is a signal that is sent to request the process terminates. The kill command sends a SIGTERM and it's a terminate. You can catch both SIGTERM and SIGINT and you will always be able to close the process with a SIGKILL or kill -9 [pid] .

What is process on in NodeJS?

The process object in Node. js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node. js and process is one of them. It is an essential component in the Node.

What is process exit in node JS?

The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS.

What is process stdout?

The process. stdout property is an inbuilt application programming interface of the process module which is used to send data out of our program. A Writable Stream to stdout. It implements a write() method.


1 Answers

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(); }); 
like image 147
Gabriel Llamas Avatar answered Sep 17 '22 15:09

Gabriel Llamas