exec_mode: cluster, pm2 running npm, logs written to main process log
What's going wrong?
Logs for my process are being sent to the output of pm2 logs and not pm2 logs app when running using cluster exec mode and using npm to launch the process.
Using pm2 to manage a process that is launched by npm (the reason for this is to use the "node" npm module that will allow the program to run with a specific version of the node binary)
How could we reproduce this issue?
If I use pm2 start npm -- start (with a package json that uses scripts: "node app.js"), this launches the process and logs are available for that process with pm2 monit or pm2 logs app. Great!
Using pm2 to launch the process with pm2 start pm2-config.json with values like below:
{
"name": "app",
"script": "npm",
"args": "start",
"cwd": "/home/nodejs/app",
"exec_mode": "cluster"
}
The application starts fine.
The application is running and pm2 shows it in pm2 list and pm2 monit.
LOGS are not available in pm2 monit or pm2 logs app.
If you use pm2 logs, you will see the app logs being written to the main process.
Supporting information
Related to closed ticket: https://github.com/Unitech/pm2/issues/3176
$ pm2 report
--- PM2 report ----------------------------------------------------------------
Date : Fri May 22 2020 13:25:36 GMT+0100 (British Summer Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 4.4.0
node version : 12.16.3
node path : /usr/bin/pm2
argv : /usr/bin/node,/usr/lib/node_modules/pm2/lib/Daemon.js
argv0 : /usr/bin/node
user : nodejs
uid : 1001
gid : 1001
uptime : 16min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 4.4.0
node version : 12.16.3
node path : /usr/bin/pm2
argv : /usr/bin/node,/usr/bin/pm2,report
argv0 : /usr/bin/node
user : nodejs
uid : 1001
gid : 1001
===============================================================================
--- System info --------------------------------------------
arch : x64
platform : linux
type : Linux
cpus : AMD EPYC 7571
cpus nb : 2
freemem : 389259264
totalmem : 4077600768
home : /home/nodejs
===============================================================================
--- PM2 list -----------------------------------------------
┌─────┬──────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 4 │ app │ default │ N/A │ cluster │ 32509 │ 12m │ 1 │ online │ 0.1% │ 43.9mb │ nodejs │ disabled │
└─────┴──────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Module
┌────┬───────────────────────────────────────┬────────────────────┬───────┬──────────┬──────┬──────────┬──────────┬──────────┐
│ id │ module │ version │ pid │ status │ ↺ │ cpu │ mem │ user │
├────┼───────────────────────────────────────┼────────────────────┼───────┼──────────┼──────┼──────────┼──────────┼──────────┤
│ 0 │ pm2-syslog │ 2.1.0 │ 29776 │ online │ 0 │ 0% │ 860.0kb │ nodejs │
└────┴───────────────────────────────────────┴────────────────────┴───────┴──────────┴──────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'
===============================================================================
--- Daemon logs --------------------------------------------
/home/nodejs/.pm2/pm2.log last 20 lines:
PM2 | Running... v8.17.0
PM2 | Running... v8.17.0
PM2 | Running... v8.17.0
PM2 | Running... v8.17.0
PM2 | Running... v8.17.0
I tried your sample and yes it writes to PM2 logs file. I wil lcheck this out. But you cannot run npm start in cluster mode anyway.. You have to specify the script to start in the "script" attribute, so PM2 will be able to use some logic to cluster your app
Thank you for picking this up so quickly. The reason we are using npm is because to use the node npm package, and specify a node binary that is included in your package, it has to be run via npm so that it checks for node in the node_modules first.
If that is not going to be an option with PM2, we will have to install nvm for the user on our host image and ask them to specify which version of node they want to use that way.
I hadn't considered using npm would cause problems with cluster mode but it makes sense that would be a problem. Hopefully by specifying the version with nvm for the user, and passing a script to run to PM2, I'll be able to have specific node versions and logging working. :+1:
Hi, Did you solve that?
Yes, I have same problem.