From 6687d499415151bd62489fed5331f414576ec354 Mon Sep 17 00:00:00 2001 From: Unitech Date: Fri, 2 Feb 2018 15:32:54 +0100 Subject: [PATCH] feat: pm2 pid command --- bin/pm2 | 6 ++++++ lib/API/Extra.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/bin/pm2 b/bin/pm2 index d8f8db781..81dca3a95 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -310,6 +310,12 @@ commander.command('startOrReload ') pm2._startJson(file, commander, 'reloadProcessId'); }); +commander.command('pid [app_name]') + .description('return pid of [app_name] or all') + .action(function(app) { + pm2.getPID(app); + }); + commander.command('startOrGracefulReload ') .description('start or gracefully reload JSON file') .action(function(file) { diff --git a/lib/API/Extra.js b/lib/API/Extra.js index 0c017ab6e..00f445dde 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -117,6 +117,34 @@ module.exports = function(CLI) { }); }; + CLI.prototype.getPID = function(app_name, cb) { + var that = this; + + if (typeof(app_name) === 'function') { + cb = app_name; + app_name = null; + } + + this.Client.executeRemote('getMonitorData', {}, function(err, list) { + if (err) { + Common.printError(cst.PREFIX_MSG_ERR + err); + return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT); + } + + var pids = []; + + list.forEach(function(app) { + if (!app_name || app_name == app.name) + pids.push(app.pid); + }) + + if (!cb) { + Common.printOut(pids.join("\n")) + return that.exitCli(cst.SUCCESS_EXIT); + } + return cb(null, pids); + }) + } /** * Create PM2 memory snapshot * @method getVersion