Skip to content

Commit

Permalink
added treeKill(pid) function
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Kim committed Sep 3, 2017
1 parent 223baa3 commit db1a5e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Retrieve or display given a process tree

`npm install -g proctree`

## Functions

* `getProcessTree(pid)` to get process tree information
* `getpids(pid)` to get process ids only in an array format
* `show(pid)` to see process trees in a text format
* `treeKill(pid)` to kill all processes in the process tree

## Usage With Bash Shell:

$ pstree 89982
Expand Down Expand Up @@ -52,4 +59,7 @@ Retrieve or display given a process tree
> PsTree.getPids(32034)
[ [ '32034' ], [ '32038' ], [ '32039' ], [ '32040' ], [ '32045', '32048', '32050' ] ]
> PsTree.treeKill(32034)
'killed 47673 47671 47668 47664 47663 47662 47658'
```
40 changes: 7 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
'use strict';
var execSync = require('child_process').execSync;
var mainPid = require('yargs').argv._[0];
/**
* Usage :
* PsTree.getProcessTree(pid); // to get process tree object
* PsTree.getPidse(pid); // to get list of process ids
* PsTree.show(pid); // to display process tree
*
* Example:
*
* > var PsTree = require('./index');
* undefined
* > PsTree.getProcessTree(32034)
* Process {
* pid: '32034',
* name: 'node index.js',
* level: 0,
* children:
* [ Process {
* pid: '32038',
* name: 'node node_modules/.bin/chromedriver --port=60397',
* level: 1,
* children: [Object] } ] }
* > PsTree.show(32034)
* * 32034 node index.js
* * 32038 node node_modules/.bin/chromedriver --port=60397
* * 32039 /Users/allen.kim/github/webtest/node_modules/chromedriver/lib/chromedriver/ch ...
* * 32040 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --disable-backgr ...
* * 32045 /Applications/Google Chrome.app/Contents/Versions/60.0.3112.113/Google Chrome ...
* * 32048 /Applications/Google Chrome.app/Contents/Versions/60.0.3112.113/Google Chrome ...
* * 32050 /Applications/Google Chrome.app/Contents/Versions/60.0.3112.113/Google Chrome ...
* > PsTree.getPids(32034)
* [ [ '32034' ], [ '32038' ], [ '32039' ], [ '32040' ], [ '32045', '32048', '32050' ] ]
*/


class Process {
constructor(pid, name, level=0) {
[this.pid, this.name, this.level, this.children] = [pid, name, level, []];
Expand Down Expand Up @@ -86,6 +54,12 @@ let PsTree = {
}
},

treeKill(pid) {
let pids = this.getPids(pid).reduce( (s, e) => s.concat(e)).reverse();
execSync(`kill -9 ${pids.join(' ')}`);
return `killed ${pids.join(' ')}`;
},

show(pid) {
console.log( this._txtProcessTree(pid) );
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proctree",
"version": "0.1.0",
"version": "0.1.1",
"description": "Retrieve or display process tree",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit db1a5e9

Please sign in to comment.