Created
January 6, 2017 21:58
-
-
Save astroza/5d5f73c89e91f5eaffa5cf573f3aa72a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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