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?
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] .
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.
The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS.
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.
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(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With