Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pthieu opened this issue Jan 5, 2016 · 4 comments
Open

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

pthieu opened this issue Jan 5, 2016 · 4 comments

Comments

@pthieu
Copy link

pthieu commented Jan 5, 2016

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
Copy link
Collaborator

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
Copy link
Author

pthieu commented Jan 18, 2016

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 billiegoose changed the title Windows: just kills process, not able to detect SIGINT "Graceful" process killing on Windows (something akin to SIGINT) Jan 30, 2016
@billiegoose
Copy link
Collaborator

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
Copy link

hey, I've update graceful to support windows.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants