From 64a9e88343685cdff5b9484e9e34d55ac2c1a28e Mon Sep 17 00:00:00 2001 From: Yoan Wainmann Date: Wed, 25 Mar 2026 18:05:47 +0200 Subject: [PATCH] fix: report error when pm2 flush targets a nonexistent process When pm2 flush is called with an app name or id that does not match any running process, it previously printed "Logs flushed" even though nothing was actually flushed. This is misleading. Now it prints an error and returns an error callback when the specified process is not found. Also adds tests for pm_id-based flushing and the not-found error case. Closes #4553 --- lib/API/LogManagement.js | 12 ++++++++- test/programmatic/flush.mocha.js | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/API/LogManagement.js b/lib/API/LogManagement.js index cdfbef1573..bc4a0fad0d 100644 --- a/lib/API/LogManagement.js +++ b/lib/API/LogManagement.js @@ -28,8 +28,12 @@ module.exports = function(CLI) { Common.printError(err); return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT); } + + var found = false; + list.forEach(function(l) { if (typeof api == 'undefined') { + found = true; Common.printOut(cst.PREFIX_MSG + 'Flushing:'); Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path); Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path); @@ -42,6 +46,7 @@ module.exports = function(CLI) { fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w')); } else if (l.pm2_env.pm_id == api || l.pm2_env.name === api) { + found = true; Common.printOut(cst.PREFIX_MSG + 'Flushing:'); if (l.pm2_env.pm_log_path && fs.existsSync(l.pm2_env.pm_log_path)) { @@ -61,7 +66,12 @@ module.exports = function(CLI) { } }); - Common.printOut(cst.PREFIX_MSG + 'Logs flushed'); + if (found) { + Common.printOut(cst.PREFIX_MSG + 'Logs flushed'); + } else if (typeof api !== 'undefined') { + Common.printError(cst.PREFIX_MSG_ERR + 'No process found with name or id \'' + api + '\''); + return cb ? cb(Common.retErr('process not found')) : that.exitCli(cst.ERROR_EXIT); + } return cb ? cb(null, list) : that.exitCli(cst.SUCCESS_EXIT); }); }; diff --git a/test/programmatic/flush.mocha.js b/test/programmatic/flush.mocha.js index a9302b76d1..2cc8322d86 100644 --- a/test/programmatic/flush.mocha.js +++ b/test/programmatic/flush.mocha.js @@ -77,5 +77,50 @@ describe('Programmatic flush feature test', function() { }); }); }); + it('flush logs by pm_id', function(done) { + pm2.start({ + script: './echo.js', + name: 'first', + error_file : 'error-first.log', + out_file : 'out-first.log', + merge_logs: false + }, function(err, procs) { + should(err).be.null(); + var out_file = procs[0].pm2_env.pm_out_log_path; + var err_file = procs[0].pm2_env.pm_err_log_path; + var pm_id = String(procs[0].pm2_env.pm_id); + pm2.start({ + script: './001-test.js', + name: 'second', + error_file : 'error-second.log', + out_file : 'out-second.log', + merge_logs: false + }, function(err, procs) { + should(err).be.null(); + var out_file1 = procs[0].pm2_env.pm_out_log_path; + var err_file1 = procs[0].pm2_env.pm_err_log_path; + pm2.flush(pm_id, function(){ + fs.readFileSync(out_file, "utf8").toString().should.be.empty(); + fs.readFileSync(err_file, "utf8").toString().should.be.empty(); + fs.readFileSync(out_file1, "utf8").toString().should.not.be.empty(); + fs.readFileSync(err_file1, "utf8").toString().should.not.be.empty(); + done(); + }); + }); + }); + }); + it('should return error when process not found', function(done) { + pm2.start({ + script: './echo.js', + name: 'myapp', + merge_logs: false + }, function(err) { + should(err).be.null(); + pm2.flush('nonexistent', function(err) { + should(err).not.be.null(); + done(); + }); + }); + }); }); }); \ No newline at end of file