Skip to content

Commit

Permalink
Use awk to filter child processes pkrumins#25
Browse files Browse the repository at this point in the history
  • Loading branch information
drdator committed Jan 9, 2019
1 parent 3b5b8fe commit b2c89bc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (pid, signal, callback) {
var pidsToProcess = {};
tree[pid] = [];
pidsToProcess[pid] = 1;

if (typeof signal === 'function' && callback === undefined) {
callback = signal;
signal = undefined;
Expand All @@ -33,7 +33,10 @@ module.exports = function (pid, signal, callback) {
// break;
default: // Linux
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn('ps', ['-o', 'pid', '--no-headers', '--ppid', parentPid]);
var ps = spawn('ps', ['-o', 'pid,ppid']);
var awk = spawn('awk', ['/ ' + parentPid + '/ {print $1}']);
ps.stdout.pipe(awk.stdin);
return awk;
}, function () {
killAll(tree, signal, callback);
});
Expand Down Expand Up @@ -88,7 +91,7 @@ function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesLi
var onClose = function (code) {
delete pidsToProcess[parentPid];

if (code != 0) {
if (code != 0 || allData == '') {
// no more parent processes
if (Object.keys(pidsToProcess).length == 0) {
cb();
Expand Down

0 comments on commit b2c89bc

Please sign in to comment.