diff --git a/README.md b/README.md index a3f9e8d..048aefa 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,14 @@ psTree(child.pid, function (err, children) { }); ``` +You can also pass more then one pid in array, and get corresponding array as result +``` js +psTree([child1.pid, child2.pid], function (err, children) { + cp.spawn('kill', ['-9'].concat(children[0].map(function (p) { return p.PID }))); + cp.spawn('kill', ['-9'].concat(children[1].map(function (p) { return p.PID }))); +}); +``` + If you prefer to run **psTree** from the command line, use: `node ./bin/ps-tree.js` ## Cross Platform support diff --git a/index.js b/index.js index 24e3284..3fbcdb5 100755 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; var spawn = require('child_process').spawn, - es = require('event-stream'); + es = require('event-stream'); module.exports = function childrenOfPid(pid, callback) { var headers = null; @@ -9,10 +9,10 @@ module.exports = function childrenOfPid(pid, callback) { if (typeof callback !== 'function') { throw new Error('childrenOfPid(pid, callback) expects callback'); } - - if (typeof pid === 'number') { - pid = pid.toString(); - } + + var pidIsArray = Array.isArray(pid) + var pids = pidIsArray ? pid : [pid] + pids = pids.map(pid => pid.toString()); // // The `ps-tree` module behaves differently on *nix vs. Windows @@ -73,17 +73,20 @@ module.exports = function childrenOfPid(pid, callback) { return cb(null, row); }), es.writeArray(function (err, ps) { - var parents = [pid], + var results = pids.map(function (pid) { + var parents = [pid], children = []; - ps.forEach(function (proc) { - if (parents.indexOf(proc.PPID) !== -1) { - parents.push(proc.PID) - children.push(proc) - } - }); + ps.forEach(function (proc) { + if (parents.indexOf(proc.PPID) !== -1) { + parents.push(proc.PID) + children.push(proc) + } + }); + return children + }) - callback(null, children); + callback(null, pidIsArray ? results : results[0]); }) ).on('error', callback) } diff --git a/test/test.js b/test/test.js index 9e655f2..5ce2a34 100644 --- a/test/test.js +++ b/test/test.js @@ -38,6 +38,34 @@ test(cyan('Spawn a Parent process which has a Two Child Processes'), function (t // need more time on a slow(or heavy load server). maybe promise.then is better instead of the timeout }); +test(cyan('Spawn Two parent processes each has a Two Child Processes'), function (t) { + var parent1 = cp.exec('node ' + scripts.parent, function (error, stdout, stderr) {}); + var parent2 = cp.exec('node ' + scripts.parent, function (error, stdout, stderr) {}); + + setTimeout(function () { + psTree([parent1.pid, parent2.pid], function (err, children) { + if (err) { console.log(err); } + console.log(red('Children: '), children, '\n'); + t.true(children[0].length > 0, green('✓ There are ' + children[0].length + ' active child processes of first')); + t.true(children[1].length > 0, green('✓ There are ' + children[1].length + ' active child processes of second')); + treeKill(parent1.pid); + treeKill(parent2.pid); + }); + + setTimeout(function () { + psTree([parent1.pid, parent2.pid], function (err, children) { + if (err) { console.log(err); } + // console.log('Children: ', children, '\n'); + // console.log(' ') + t.equal(children[0].length, 0, green('✓ No more active child processes of first(we killed them)')); + t.equal(children[1].length, 0, green('✓ No more active child processes of second (we killed them)')); + t.end(); + }); + }, 2000); // give psTree time to kill the processes + }, 500); // give the child process time to spawn + // need more time on a slow(or heavy load server). maybe promise.then is better instead of the timeout +}); + test(cyan('FORCE ERROR by calling psTree without supplying a Callback'), function (t) { var errmsg = 'Error: childrenOfPid(pid, callback) expects callback' // Attempt to call psTree without a callback