pkrumins / node-tree-kill

kill trees of processes
MIT License
335 stars 37 forks source link

"Graceful" process killing on Windows (something akin to SIGINT) #13

Open pthieu opened 8 years ago

pthieu commented 8 years ago

I get that Windows doesn't handle POSIX signals like Linux, but seems like the process just gets killed so you can't do a graceful shutdown.

Any way to do this? Let's say I have a process that's doing it's own thing and want to kill it, but before it does, I want it to do something:

'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);
billiegoose commented 8 years ago

I looked into supporting a CTRL_C "signal" for Windows platforms, but it looks like existing solutions are hacky at best. Source: http://stanislavs.org/stopping-command-line-applications-programatically-with-ctrl-c-events-from-net/

If there is a universal way to gracefully shutdown windows processes, I'm not aware of it.

For your particular case though, if the process you are trying to kill is also a Node program and you can edit the source, it should be easy to use process.send() or some other mechanism to communicate with the process and tell it to stop. I don't think that's within the scope of tree-kill though.

pthieu commented 8 years ago

Yes, from my findings, it's pretty much impossible to gracefully kill in windows from a separate process, at least for the time being. Unless node implements a way to hook into its own processes like how some IDEs work when debugging node.js applications, we're stuck with hacky methods that don't fully work.

billiegoose commented 8 years ago

I'm going to officially move this into the "Features I would like tree-kill to support, but would need a volunteer to actually figure out how" category.

feifeipan commented 6 years ago

hey, I've update graceful to support windows.

please see https://github.com/feifeipan/childprocess-exit for details.