Skip to content

Instantly share code, notes, and snippets.

@astroza
Created January 6, 2017 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astroza/5d5f73c89e91f5eaffa5cf573f3aa72a to your computer and use it in GitHub Desktop.
Save astroza/5d5f73c89e91f5eaffa5cf573f3aa72a to your computer and use it in GitHub Desktop.
var cp = require('child_process');
function *doKill(process, initial_delay)
{
yield process.kill('SIGTERM');
yield process.kill('SIGKILL');
}
function niceKill(process, onExit, initial_delay)
{
var k = doKill(process);
process.on('exit', () => {
this.cancel();
onExit && onExit(process);
});
if(!initial_delay)
k.next();
this.kInt = setInterval(() => k.next(), 5000);
}
niceKill.prototype.cancel = function()
{
clearInterval(this.kInt);
}
var command = '/bin/sleep';
var arguments = ['3600'];
var n = cp.spawn(command, arguments, {
stdio: [
'ignore', // use parents stdin for child
'ignore', // pipe child's stdout to parent
'pipe' // direct child's stderr to a file
]
});
var a = new niceKill(n, (n) => console.log(n.killed), true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment