/*1* common.js: Common methods used in `forever-monitor`.2*3* (C) 2010 Nodejitsu Inc.4* MIT LICENCE5*6*/78var psTree = require('ps-tree'),9spawn = require('child_process').spawn;1011//12// ### function checkProcess (pid, callback)13// #### @pid {string} pid of the process to check14// #### @callback {function} Continuation to pass control backto.15// Utility function to check to see if a pid is running16//17exports.checkProcess = function (pid) {18if (!pid) {19return false;20}2122try {23//24// Trying to kill non-existent process here raises a ESRCH - no such25// process exception. Also, signal 0 doesn't do no harm to a process - it26// only checks if sending a singal to a given process is possible.27//28process.kill(pid, 0);29return true;30}31catch (err) {32return false;33}34};3536exports.kill = function(pid, killTree, callback) {37if (killTree) {38psTree(pid, function (err, children) {39var pids = children.map(function (p) {40return p.PID;41});4243pids.unshift(pid);44spawn('kill', ['-9'].concat(pids)).on('exit', callback || function() {});45});46}47else {48try {49process.kill(pid);50}51catch (ex) { }52callback && callback();53}54};5556