The Problem
When you deploy PM2 managed apps (Node.js, Python, etc.) on Linux system with SELinux + Systemd, you will find the PM2 process got killed every 1~2 minutes.
https://stackoverflow.com/questions/62814539/pm2-keeps-getting-killed-every-90-seconds-on-centos-8
https://github.com/Unitech/pm2/issues/4813
Since PM2 itself is killed, all managed Node.js/Python app instances will be killed, too. PM2 will auto restart after being killed, so this problem is hard to notice. You may see occasionally 502 errors during the restarting time window.
The Cause
pm2 startup will create a systemd system service. This systemd service read pm2 saved data under user’s home directory. (Default: /home/your_user/.pm2/) However, SELinux thinks this is a bad act and deny access, causing system service fail and restart. systemd will send kill signal to PM2, causing PM2 killed and restarted.
The Solution
The most easy and safe solution is to modify SELinux rules to allow Systemd read ~/.pm2 directory.
# 1. Define the rule in the SELinux database
sudo semanage fcontext -a -t var_run_t "/home/<your_user>/.pm2(/.*)?"
# 2. Apply the rule to the existing files
sudo restorecon -Rv /home/<your_user>/.pm2
Leave a Reply