I am new to node.js and following some tutorials. In one of them, I execute the following code and expect to shutdown the terminal gracefully which is not the case
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
process.stdout.write('Data! -> ' + chunk);
});
process.stdin.on('end', function() {
process.stderr.write('End!\n');
});
process.on('SIGTERM', function() {
process.stderr.write("Why are you trying to terminate me?!? :-)");
});
console.log("Node is running as process #" + process.pid);
It works fine, but when I issue, from another terminal the following
taskkill /PID 29884 /F
I don't get the function
process.on('SIGTERM', function()....
get executed.
I came across a thread here at What is the Windows equivalent of process.on('SIGINT') in node.js?
I get the same behavior, killing the process just goes back to the command line
Then I tried to update the code I got from the thread with some code in order to reprint the Data I enter on the console, it is not getting there (I added rl.on("data", function(chunk))
(process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.output.setEncoding('utf8');
console.log("Node is running as process #" + process.pid);
rl.on("data", function(chunk) {
this.output.write('Data! -> ' + chunk);
});
rl.on("SIGINT", function() {
process.emit("SIGINT");
});
};
process.on("SIGINT", function() {
//graceful shutdown
process.exit();
});
The
rl.on("data", function(chunk) {
this.output.write('Data! -> ' + chunk);
});
just sends back 'Data! -> ' string without the text I enter in the console as the case with the 1st code from the tutorial. What is missing? Of course, In both cases
process.stderr.write("Why are you trying to terminate me?!? :-)");
is not getting executed Thanks