pm2 - 非 root 用户权限的启动脚本被拒绝

pm2 - Startup script for non-root user permission denied

我刚刚安装了 pm2(在 Debian 9 上),我需要 运行 几个用户的 NodeJS 服务器。每个用户都有自己的 NodeJS 服务器,我需要在它们下面 运行。不在 root 下。

例如我有用户 "user1":

pm2 仍然可以在 user1 上工作,但重启后我无法控制 pm2,我收到此错误: [PM2][ERROR] Permission denied, to give access to current user: $ sudo chown user1:user1 /home/user1/.pm2/rpc.sock /home/user1/.pm2/pub.sock

如果我在 root 上执行此命令,我可以再次控制 user1 上的 pm2,但是所有 NodeJS 服务器都以 root 执行!

每次重启我都需要'grant'权限..

如何解决这个问题?

我自己发现了为什么它不起作用。我的用户名带有点 (domain.com) 并且 systemd 无法将其识别为有效用户名...因为该服务是在 root 下启动的。

快速修复是在 systemd 中找到 auto-generated 服务并将用户名替换为 id -u <user>

找到的用户 ID

我不确定以下方法是否存在任何风险,但它确实解决了我面临的同样问题。

1- open new terminal (fresh, without sudo permission)
2- sudo chmod -R 777 /home/user1/.pm2 (gives permission to the .pm2 folder)
3- pm2 start index.js (Note that there is no sudo here)
4- pm2 save
5- pm2 startup
6- Restart your computer and check pm2 monit
7- It should list your index process now. If not, try from 1-5, 8, and then 6.
8- paste the command listed in step 5

您可能使用 root 启动了 pm2 任务。因此,请通过以下方式检查您正在使用的系统进程:

sudo systemctl -a

如果您找到一个名为 pm2-root 的单元,那就是您需要删除的单元。 运行 命令 sudo systemctl stop pm2-root 和 sudo systemctl disable pm2-root。然后使用您的用户名开始该过程。

确保您使用的 pm2-sammysammy 作为您的用户名。

尝试使用 sudo pm2 <command>