Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing mitiple pids #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'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;

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
Expand Down Expand Up @@ -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)
}
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down