2

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

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
eliassal
  • 343
  • 5
  • 20
  • 1
    I don't know how node.js maps console events to C signals, but in Microsoft's C runtime, `SIGINT` is mapped to `CTRL_C_EVENT` (cancel via Ctrl+C) and all others are mapped to `SIGBREAK`, which includes `CTRL_BREAK_EVENT` (Ctrl+Break) and `CTRL_CLOSE_EVENT` (the console session is closing). The C runtime only supports `SIGTERM` internally within the current process via `raise(SIGTERM)`, so it's really pointless. – Eryk Sun May 08 '20 at 08:59
  • 2
    Windows itself does not have Unix signals. taskkill.exe with `/F` calls `TerminateProcess`, which is like Unix `SIGKILL`; it immediately terminates the process and cannot be handled. Without `/F`, taskkill tries to send `WM_CLOSE` to top-level and message-only windows of a process -- the Windows equivalent of `SIGTERM`. But a console app usually has no windows, unless it owns the console window, which typically means that the process allocated the console, either implicitly or via `AllocConsole`. – Eryk Sun May 08 '20 at 09:08
  • @ErykSun, thanks for mentioning Ctrl+Break! For whatever reason only this combination worked for me in Windows cmd console. Ctrl+C worked in PowerShell and Terminal windows though. – Егор Малыгин Feb 22 '23 at 07:35

0 Answers0