main
|
Last change
on this file since 2d1ec46 was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
857 bytes
|
| Line | |
|---|
| 1 | const spawn = require('child_process').spawn;
|
|---|
| 2 |
|
|---|
| 3 | module.exports = function (rootPid, callback) {
|
|---|
| 4 | const pidsOfInterest = new Set([parseInt(rootPid, 10)]);
|
|---|
| 5 | var output = '';
|
|---|
| 6 |
|
|---|
| 7 | // *nix
|
|---|
| 8 | const ps = spawn('ps', ['-A', '-o', 'ppid,pid']);
|
|---|
| 9 | ps.stdout.on('data', (data) => {
|
|---|
| 10 | output += data.toString('ascii');
|
|---|
| 11 | });
|
|---|
| 12 |
|
|---|
| 13 | ps.on('close', () => {
|
|---|
| 14 | try {
|
|---|
| 15 | const res = output
|
|---|
| 16 | .split('\n')
|
|---|
| 17 | .slice(1)
|
|---|
| 18 | .map((_) => _.trim())
|
|---|
| 19 | .reduce((acc, line) => {
|
|---|
| 20 | const pids = line.split(/\s+/);
|
|---|
| 21 | const ppid = parseInt(pids[0], 10);
|
|---|
| 22 |
|
|---|
| 23 | if (pidsOfInterest.has(ppid)) {
|
|---|
| 24 | const pid = parseInt(pids[1], 10);
|
|---|
| 25 | acc.push(pid);
|
|---|
| 26 | pidsOfInterest.add(pid);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | return acc;
|
|---|
| 30 | }, []);
|
|---|
| 31 |
|
|---|
| 32 | callback(null, res);
|
|---|
| 33 | } catch (e) {
|
|---|
| 34 | callback(e, null);
|
|---|
| 35 | }
|
|---|
| 36 | });
|
|---|
| 37 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.