Skip to content

Commit

Permalink
Merge pull request #20 from jedwards1211/process-kill
Browse files Browse the repository at this point in the history
use process.kill instead of spawning linux kill
  • Loading branch information
nelsonic authored Oct 27, 2016
2 parents 8af86a9 + 0fe368c commit 8f0a50e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions terminate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var cp = require('child_process');
var psTree = require('ps-tree'); // see: http://git.io/jBHZ

/**
Expand All @@ -17,13 +16,22 @@ module.exports = function terminate(pid, callback) {
throw new Error("No pid supplied to Terminate!")
}
psTree(pid, function (err, children) {
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
.on('exit', function() {
if(callback && typeof callback === 'function') {
callback(err, true);
} else { // do nothing
console.log(children.length + " Processes Terminated!");
}
});
children.forEach(function (child) {
try {
process.kill(parseInt(child.PID), 'SIGKILL');
} catch (error) {
// ignore
}
});
try {
process.kill(pid, 'SIGKILL');
} catch (error) {
// ignore
}
if(callback && typeof callback === 'function') {
callback(err, true);
} else { // do nothing
console.log(children.length + " Processes Terminated!");
}
});
};

0 comments on commit 8f0a50e

Please sign in to comment.