-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathterminate.js
54 lines (48 loc) · 1.63 KB
/
terminate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
var psTree = require('ps-tree'); // see: https://git.io/jBHZ
var handlePsTreeCallback = require('./handlePsTreeCallback');
/**
* terminate is an ultra-simple way to kill all the node processes
* by providing a process pid. It finds all child processes and shuts
* them down too, so you don't have to worry about lingering processes.
* @param {int} pid - the Process ID you want to terminate
* @param {string} [signal=SIGKILL] - the signal to kill the processes
* with.
* @param {object} [options={}] - options object
* @param {number} [options.pollInterval=500] - interval to poll whether pid
* and all of the child pids have been killed.
* @param {number} [options.timeout=5000] - max time to wait for processes to
* exit before timing out and calling back with an error.
* @param {function=} callback - if you want to know once the
* procesess have been terminated, supply a callback.
* @param {Error} error - will be null if no error occured
* @returns {void}
*/
module.exports = function terminate (pid) {
var i = 1;
var signal, options, callback;
if (!pid) {
throw new Error('No pid supplied to Terminate!');
}
// extract optional arguments. Any can be omitted, but the
// ones present have to be in the
if (typeof arguments[i] === 'string') {
signal = arguments[i++];
}
if (typeof arguments[i] === 'object' && arguments[i]) {
options = arguments[i++];
}
if (typeof arguments[i] === 'function') {
callback = arguments[i++];
}
psTree(pid, function (err, children) {
handlePsTreeCallback(
err,
children,
pid,
signal,
options,
callback
);
});
};