Graceful kill off a detached node.js spawned child process in Windows
<p>Not a duplicate of <a href="https://stackoverflow.com/questions/23706055/why-can-i-not-kill-my-child-process-in-nodejs-on-windows">this</a>: I can kill process fine, I want to know how to detect, within the process, that it's being killed, and gracefully shutdown.</p>

<h1><strong>Overview:</strong></h1>

<p>I have a CLI tool to spawn and kill a child node.js process. The demo code is contained in these three files:</p>

<p><code>spawn.js</code> -- Will spawn the <code>child.js</code> script, detached. For simplicity, I pipe the child's <code>stdio</code> to an <code>out.log</code> file</p>

<p><code>child.js</code> -- A simple counter which writes to a file, <strong>uses the <code>readline</code> method to detect an emulated SIGINT in Windows</strong></p>

<p><code>kill.js</code> -- Invokes a <code>process.kill()</code> on the child process, using it's PID</p>

<hr>

<h1><strong>Code:</strong></h1>

<p><strong>spawn.js</strong></p>

<pre><code>'use strict';

var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');

var childFilePath = path.resolve(__dirname, 'child.js');

var out = fs.openSync('./out.log', 'a');
var err = fs.openSync('./out.log', 'a');

var options = {
  detached: true,
  stdio: ['ignore', out, err],
};

var child = spawn(process.execPath, [childFilePath], options);
child.unref();
</code></pre>

<p><strong>child.js</strong></p>

<pre><code>'use strict';

var fs = require('fs');
var path = require('path');

if (process.platform === 'win32') {
  console.log('win32 true');
  var rl = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.on('SIGINT', function() {
    process.emit('SIGINT');
  });
}

process.on('SIGINT', function() {
  console.log('SIGINT');
  process.exit();
});

var filepath = path.resolve(__dirname, 'pid.txt');

fs.writeFile(filepath, process.pid);

var i = 0;
setInterval(function () {
  console.log(i++);
}, 1000);
</code></pre>

<p><strong>kill.js</strong></p>

<pre><code>'use strict';

var fs = require('fs');
var path = require('path');


var pidPath = path.resolve(__dirname, 'pid.txt');


fs.readFile(pidPath, 'utf8', function (err, data) {
  if (err) {
    return console.log(err);
  }
  process.kill(data, 'SIGINT');
});
</code></pre>

<hr>

<h1><strong>Problem:</strong></h1>

<p>When sending the <code>process.kill(PID, 'SIGINT')</code>, it doesn't actually detect it as a <code>SIGINT</code> in windows. I can run <code>child.js</code> manually and use <code>CTRL+C</code> to kill the process to trigger a <code>SIGINT</code>, so I know that the <code>readline</code> code is working (or maybe not since <code>SIGINT</code> will trigger without the <code>readline</code> code, but it will nonetheless trigger a <code>SIGINT</code>)</p>

<p>Does <code>process.kill()</code> not send the type of signal to a detached process? How do I detect that that a separate script is trying to kill my child process and shutdown gracefully?</p>