Skip to content

Commit 7f1c29f

Browse files
committed
[dist fix] Give in to more code style OCD.
1 parent b07fc23 commit 7f1c29f

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

README.md

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
11
#ps-tree
22

3-
sometimes you cannot kill child processes like you would expect,
4-
this a feature of UNIX.
3+
Sometimes you cannot kill child processes like you would expect, this a feature of UNIX.
54

6-
>in UNIX, a process may terminate by using the exit call, and it's parent process may wait for that event by using the wait system call. the wait system call returns the process identifier of a terminated child, so that the parent tell which of the possibly many children has terminated. If the parent terminates, however, all it's children have assigned as their new parent the init process. Thus, the children still have a parent to collect their status and execution statistics.
5+
>in UNIX, a process may terminate by using the exit call, and it's parent process may wait for that event by using the wait system call. the wait system call returns the process identifier of a terminated child, so that the parent tell which of the possibly many children has terminated. If the parent terminates, however, all it's children have assigned as their new parent the init process. Thus, the children still have a parent to collect their status and execution statistics.
76
> (from "operating system concepts")
87
9-
solution: use `ps-tree` to get all processes that a child_process may have started, so that they may all be terminated.
8+
Solution: use `ps-tree` to get all processes that a `child_process` may have started, so that they may all be terminated.
109

1110
``` js
1211
var cp = require('child_process'),
1312
psTree = require('ps-tree')
1413

1514
var child = cp.exec("node -e 'while (true);'",function () {...})
1615

17-
child.kill() //this will not actually kill the child it will kill the `sh` process.
18-
16+
// This will not actually kill the child it will kill the `sh` process.
17+
child.kill();
1918
```
2019

2120
wtf? it's because exec actually works like this:
2221

2322
``` js
2423
function exec (cmd, cb) {
25-
spawn('sh', ['-c', cmd])
24+
spawn('sh', ['-c', cmd]);
2625
...
2726
}
28-
2927
```
3028

31-
sh starts parses the command string and starts processes, and waits for them to terminate.
32-
but exec returns a process object with the pid of the sh process.
33-
but since it is in `wait` mode killing it does not kill the children.
29+
`sh` starts parses the command string and starts processes, and waits for them to terminate, but `exec` returns a process object with the pid of the `sh` process.
30+
However, since it is in `wait` mode killing it does not kill the children.
3431

35-
used ps tree like this:
32+
Use `ps-tree` like this:
3633

3734
``` js
3835
var cp = require('child_process'),
39-
psTree = require('ps-tree')
36+
psTree = require('ps-tree');
4037

41-
var child = cp.exec("node -e 'while (true);'",function () {...})
38+
var child = cp.exec("node -e 'while (true);'", function () { /*...*/ });
4239

4340
psTree(child.pid, function (err, children) {
44-
cp.spawn('kill', ['-9'].concat(children.map(function (p) {return p.PID})))
45-
})
46-
41+
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })));
42+
});
4743
```
4844

49-
If you prefer to run **psTree** from the command line,
50-
use: `node ./bin/ps-tree.js`
51-
45+
If you prefer to run **psTree** from the command line, use: `node ./bin/ps-tree.js`
5246

5347
[![Build Status](https://travis-ci.org/nelsonic/ps-tree.svg)](https://travis-ci.org/nelsonic/ps-tree)
5448
[![Code Climate](https://codeclimate.com/github/nelsonic/ps-tree/badges/gpa.svg)](https://codeclimate.com/github/nelsonic/ps-tree)

bin/ps-tree.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env node
2+
23
'use strict';
4+
35
require('../')(process.argv[2] || 1, function (err, data) {
4-
console.log(data)
6+
console.log(data);
57
});

index.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1+
'use strict';
2+
13
var spawn = require('child_process').spawn,
24
es = require('event-stream');
35

4-
module.exports = childrenOfPid
5-
6-
function childrenOfPid( pid, callback) {
6+
module.exports = function childrenOfPid(pid, callback) {
77
var headers = null;
88

9-
if('function' !== typeof callback)
10-
throw new Error('childrenOfPid(pid, callback) expects callback')
11-
if('number' == typeof pid)
12-
pid = pid.toString()
9+
if (typeof callback !== 'function') {
10+
throw new Error('childrenOfPid(pid, callback) expects callback');
11+
}
12+
13+
if (typeof pid === 'number') {
14+
pid = pid.toString();
15+
}
1316

1417
es.connect(
1518
spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm']).stdout,
1619
es.split(),
1720
es.map(function (line, cb) { //this could parse alot of unix command output
18-
var columns = line.trim().split(/\s+/)
19-
if(!headers)
20-
headers = columns
21-
else {
22-
var row = {}
23-
//for each header,
24-
var h = headers.slice()
25-
while (h.length) {
26-
row[h.shift()] = h.length ? columns.shift() : columns.join(' ')
27-
}
28-
return cb(null, row)
21+
var columns = line.trim().split(/\s+/);
22+
if (!headers) {
23+
headers = columns;
24+
return cb();
2925
}
30-
return cb()
26+
27+
var row = {};
28+
// For each header
29+
var h = headers.slice();
30+
while (h.length) {
31+
row[h.shift()] = h.length ? columns.shift() : columns.join(' ');
32+
}
33+
34+
return cb(null, row);
3135
}),
3236
es.writeArray(function (err, ps) {
33-
var parents = [pid], children = []
37+
var parents = [pid],
38+
children = [];
39+
3440
ps.forEach(function (proc) {
35-
if(-1 != parents.indexOf(proc.PPID)) {
41+
if (parents.indexOf(proc.PPID) !== -1) {
3642
parents.push(proc.PID)
3743
children.push(proc)
3844
}
39-
})
40-
callback(null, children)
45+
});
46+
47+
callback(null, children);
4148
})
4249
).on('error', callback)
4350
}

0 commit comments

Comments
 (0)