pstree.remy
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -29,5 +29,5 @@ const exec = require('child_process').exec; | ||
if (!module.parent) { | ||
module.exports(process.argv[2], pids => console.log(pids)); | ||
module.exports(process.argv[2], (e, pids) => console.log(pids)); | ||
} | ||
module.exports.hasPS = hasPS; |
118
lib/tree.js
@@ -1,96 +0,34 @@ | ||
'use strict'; | ||
const spawn = require('child_process').spawn; | ||
/** | ||
* adapted from https://github.com/pkrumins/node-tree-kill | ||
* code credit to @pkrumins | ||
* @remy has just made a few adjustments to remove unneeded functionality | ||
*/ | ||
module.exports = function(rootPid, callback) { | ||
const tree = {}; | ||
let output = ''; | ||
var childProcess = require('child_process'); | ||
var spawn = childProcess.spawn; | ||
var exec = childProcess.exec; | ||
// *nix | ||
const ps = spawn('ps', ['-A', '-o', 'ppid,pid']); | ||
ps.stdout.on('data', data => { | ||
output += data.toString('ascii'); | ||
}); | ||
module.exports = function(pid, callback) { | ||
var tree = {}; | ||
var pidsToProcess = {}; | ||
tree[pid] = []; | ||
pidsToProcess[pid] = 1; | ||
ps.on('close', () => { | ||
try { | ||
const res = output | ||
.split('\n') | ||
.slice(1) | ||
.map(_ => _.trim()) | ||
.reduce((acc, line) => { | ||
if (line.indexOf(rootPid + ' ') === 0) { | ||
const pid = line.split(/\s+/).pop(); | ||
acc.push(parseInt(pid, 10)); | ||
rootPid = pid; | ||
} | ||
switch (process.platform) { | ||
case 'darwin': | ||
buildProcessTree( | ||
pid, | ||
tree, | ||
pidsToProcess, | ||
function(parentPid) { | ||
return spawn('pgrep', ['-P', parentPid]); | ||
}, | ||
function() { | ||
callback(null, Object.keys(tree)); | ||
} | ||
); | ||
break; | ||
// case 'sunos': | ||
// buildProcessTreeSunOS(pid, tree, pidsToProcess, function () { | ||
// killAll(tree, signal, callback); | ||
// }); | ||
// break; | ||
default: | ||
// Linux | ||
buildProcessTree( | ||
pid, | ||
tree, | ||
pidsToProcess, | ||
function(parentPid) { | ||
return spawn('ps', [ | ||
'-o', | ||
'pid', | ||
'--no-headers', | ||
'--ppid', | ||
parentPid, | ||
]); | ||
}, | ||
function() { | ||
callback(null, Object.keys(tree)); | ||
} | ||
); | ||
break; | ||
} | ||
}; | ||
return acc; | ||
}, []); | ||
function buildProcessTree( | ||
parentPid, | ||
tree, | ||
pidsToProcess, | ||
spawnChildProcessesList, | ||
cb | ||
) { | ||
var ps = spawnChildProcessesList(parentPid); | ||
var allData = ''; | ||
ps.stdout.on('data', function(data) { | ||
var data = data.toString('ascii'); | ||
allData += data; | ||
callback(null, res); | ||
} catch (e) { | ||
callback(e, null); | ||
} | ||
}); | ||
var onClose = function(code) { | ||
delete pidsToProcess[parentPid]; | ||
if (code != 0) { | ||
// no more parent processes | ||
if (Object.keys(pidsToProcess).length == 0) { | ||
cb(); | ||
} | ||
return; | ||
} | ||
allData.match(/\d+/g).forEach(function(pid) { | ||
pid = parseInt(pid, 10); | ||
tree[parentPid].push(pid); | ||
tree[pid] = []; | ||
pidsToProcess[pid] = 1; | ||
buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb); | ||
}); | ||
}; | ||
ps.on('close', onClose); | ||
} | ||
}; |
{ | ||
"name": "pstree.remy", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "NO_PS=1 tap tests/*.test.js" | ||
"test": "tap tests/*.test.js" | ||
}, | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -8,6 +8,8 @@ const tap = require('tap'); | ||
tap.test('reads from /proc', async t => { | ||
const ps = await getStat(); | ||
t.ok(ps.split('\n').length > 1); | ||
}); | ||
if (process.platform !== 'darwin') { | ||
test('reads from /proc', async t => { | ||
const ps = await getStat(); | ||
t.ok(ps.split('\n').length > 1); | ||
}); | ||
} | ||
@@ -22,27 +24,19 @@ test('tree for live env', async t => { | ||
test('can read full child process tree', t => { | ||
const sub = spawn( | ||
'sh', | ||
[ | ||
'-c', | ||
`node -e "setInterval(() => { | ||
console.log(process.pid, 'is alive') | ||
}, 200);"`, | ||
], | ||
{ | ||
stdio: 'pipe', | ||
} | ||
); | ||
console.log('parent at %s', sub.pid); | ||
const sub = spawn('node', [`${__dirname}/fixtures/index.js`], { | ||
stdio: 'pipe', | ||
}); | ||
setTimeout(() => { | ||
const pid = sub.pid; | ||
pstree(pid, (error, children) => { | ||
children.concat({ PID: pid }).forEach(p => { | ||
spawn('kill', ['-s', 'SIGTERM', p.PID]); | ||
children.concat([pid]).forEach(p => { | ||
spawn('kill', ['-s', 'SIGTERM', p]); | ||
}); | ||
t.equal(children.length, 1); | ||
console.log(children); | ||
t.equal(children.length, 2); | ||
t.end(); | ||
}); | ||
}, 500); | ||
}, 1000); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
12606
143
5