Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
/*
2
* common.js: Common methods used in `forever-monitor`.
3
*
4
* (C) 2010 Nodejitsu Inc.
5
* MIT LICENCE
6
*
7
*/
8
9
var psTree = require('ps-tree'),
10
spawn = require('child_process').spawn;
11
12
//
13
// ### function checkProcess (pid, callback)
14
// #### @pid {string} pid of the process to check
15
// #### @callback {function} Continuation to pass control backto.
16
// Utility function to check to see if a pid is running
17
//
18
exports.checkProcess = function (pid) {
19
if (!pid) {
20
return false;
21
}
22
23
try {
24
//
25
// Trying to kill non-existent process here raises a ESRCH - no such
26
// process exception. Also, signal 0 doesn't do no harm to a process - it
27
// only checks if sending a singal to a given process is possible.
28
//
29
process.kill(pid, 0);
30
return true;
31
}
32
catch (err) {
33
return false;
34
}
35
};
36
37
exports.kill = function(pid, killTree, callback) {
38
if (killTree) {
39
psTree(pid, function (err, children) {
40
var pids = children.map(function (p) {
41
return p.PID;
42
});
43
44
pids.unshift(pid);
45
spawn('kill', ['-9'].concat(pids)).on('exit', callback || function() {});
46
});
47
}
48
else {
49
try {
50
process.kill(pid);
51
}
52
catch (ex) { }
53
callback && callback();
54
}
55
};
56