From f82005655962a206f06230d55bfffdd913228c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Mon, 10 Oct 2011 21:22:38 +0200 Subject: [PATCH] [fix] Use `process.kill` to check if process is alive Old solution spawned `ps | grep -v PID` to perform this check. --- lib/forever.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index de71f82d..73ba778d 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -853,13 +853,18 @@ forever.checkProcess = function (pid, callback) { return callback(false); } - exec('ps ' + pid + ' | grep -v PID', function (err, stdout, stderr) { - if (err) { - return callback(false); - } - - callback(stdout.indexOf(pid) !== -1); - }); + try { + // + // Trying to kill non-existent process here raises a ESRCH - no such + // process exception. Also, signal 0 doesn't do no harm to a process - it + // only checks if sending a singal to a given process is possible. + // + process.kill(pid, 0); + callback(true); + } + catch (err) { + callback(false); + } }; // @@ -910,4 +915,4 @@ forever.columns = { return timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow; } } -}; \ No newline at end of file +};