PM2 startup does not respect PM2 environment variables
What's going wrong?
For production servers, it is often important to put pids in specific locations (SELinux concerns) and logging on specific mount points. You folk nicely added PM2_LOG_FILE_PATH and PM2_PID_FILE_PATH, and thanks (though not flags? eh whatever), when running PM2 on its own this works nicely. Also, that the ecosystem file can also have its own pid/log info set, great. Except...
pm2 startup, however, completely ignores these environment variables when generating service files. In fact, this precipitates constant service restarts if your SELinux policy blocks reads of pid files in home directories (thanks CentOS 8.0?). As you can guess, very annoying getting intermittent connections reset by peer. One could setenforce 0, but that's a bad idea, so having PIDs written to somewhere standard like /var/run is ideal to just not have to make your SELinux policies worse than they already are. Except of course, this bug prevents that without manual intervention of the generated service file.
Obviously, this is workaround-able by not using pm2 startup, but it'd be great if pm2 startup did the right thing and passed the PM2_* environment variables along to the service definition, so when the pm2 daemon starts, you don't end up getting unexpected behaviors. You know, go to the winchester, have a pint, and wait for the deployment to blow over.
How could we reproduce this issue?
-
PM2_LOG_FILE_PATH=/var/logs/pm2.log PM2_PID_FILE_PATH=/var/run/pm2.pid pm2 startup - Execute the command it shows (note the lack of PM2_* variables except for PM2_HOME). Or even try to execute the pm2 startup command where you explicitly add your PM2_* variables yourself.
- Open the service definition, and see that neither PM2_LOG_FILE_PATH or PM2_PID_FILE_PATH are set as environment variables. Leading pm2 to be invoked incorrectly when you start the service.
- Take a deep breath, sigh, contemplate how you got here in life, and just do it yourself.
On CentOS 8.0, you will see executing systemctl status pm2-$USER is showing errors causing the PM2 service to endlessly restart every minute due to a SELinux permission issue involving the PID file that is ~/.pm2/pm2.pid, not the PM2_PID_FILE_PATH you attempted to specify.
Supporting information
Date : Mon Aug 03 2020 17:02:55 GMT-0400 (Eastern Daylight Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 4.4.0
node version : 12.18.3
node path : /usr/local/bin/pm2
argv : /usr/bin/node,/usr/local/share/.config/yarn/global/node_modules/pm2/lib/Daemon.js
argv0 : node
user : <redacted>
uid : 1000
gid : 1000
uptime : 7min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 4.4.0
node version : 12.18.3
node path : /usr/local/bin/pm2
argv : /usr/bin/node,/usr/local/bin/pm2,report
argv0 : node
user : <redacted>
uid : 1000
gid : 1000
===============================================================================
--- System info --------------------------------------------
arch : x64
platform : linux
type : Linux
cpus : Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz
cpus nb : 8
freemem : 1248415744
totalmem : 3948318720
home : /home/<redacted>
===============================================================================```
Why this Issue was closed without any resolution ?