Pm2: ~/.pm2 is created with root as the owner and completely breaks pm2

Created on 21 Nov 2014  路  7Comments  路  Source: Unitech/pm2

start with a server that has never had pm2 installed (the important part is that there is no ~/.pm2)

npm install pm2 -g
sudo env PATH=$PATH:`dirname $(which pm2)` pm2 startup ubuntu -u vagrant

at that point, ~/.pm2 has a root owner and if you try to do anything with pm2 after that (e.g. pm2 ls) you'll get an EACCES error.

my workaround is

npm install pm2 -g
# prevent pm2 from creating ~/.pm2 as the root user
pm2 ls
sudo env PATH=$PATH:`dirname $(which pm2)` pm2 startup ubuntu -u vagrant

this is possibly related to #789

Bug

Most helpful comment

I've run into the same thing. I found that this created the init script correctly, and is more elegant than editing the init script:

PM2_HOME=/home/myuser/.pm2 pm2 startup ubuntu -u myuser

Unfortunately this command starts a pm2 God daemon before the init script is created, so I'm left with a running pm2 daemon with the wrong settings owned by root that breaks everything because it points at /root/.pm2, as above.

The next problem is that it's hard to stop this daemon nicely because it looks in the wrong place for its config because the init script is now in effect (i.e. service pm2-init.sh stop doesn't work), so I have to aggressively pkill -f PM2 to get rid of it before a simple service pm2-init.sh start starts it with the correct user, pointing at the correct .pm2 folder. Phew.

A minor issue - why not rename the init script to just pm2 when installing it, like nearly all other services do?

All 7 comments

Also related to #712 where pm2 init itself (https://github.com/Unitech/PM2/blob/master/lib/CLI.js#L31).

I think this should be called at the npm install stage, so that pm2 already has the $PM2_HOME directory when installation is finished.
It'd be the best way to prevent EACCESS error because if you run the startup as sudo it'll indeed create the directory as root if you haven't called the CLI at least once.

A similar use case will be that pm2 is a dependency and that you call pm2.connect() before invoking pm2 -v or in your case pm2 ls.

837 read this too about startup and pm2 user owner ;)

872

An easy solution is to add pm2 -v (or any pm2 command) in the postinstall script so that PM2 sets it's home directory after the npm install command.

Hello,

I ran into this issue as well and had to perform a hack to make it work for me.

sudo -i pm2 dump
sudo -i pm2 startup ubuntu

if the start up file exists already it must be manually removed as follows:

sudo rm /etc/init.d/pm2-init.sh
sudo update-rc.d pm2-init.sh remove

now clear the contents of /root/ pm2 configuration and copy the user configuration over

sudo rm -Rf /root/.pm2
sudo cp -Rf .pm2 /root/.pm2

than i needed to modify the init script as follows to get it to work

sudo nano /etc/init.d/pm2-init.sh

cat /etc/init.d/pm2-init.sh

NAME=pm2
PM2=/usr/local/lib/node_modules/pm2/bin/pm2
USER=root
HOME=/user/ubuntu
PM2_HOME=/user/ubuntu/.pm2
export PATH=$PATH:/usr/bin
export PM2_HOME="/user/ubuntu/.pm2"

please notice this fix above which resolved the issue

PM2_HOME=/user/ubuntu/.pm2
export PM2_HOME="/user/ubuntu/.pm2"

I've run into the same thing. I found that this created the init script correctly, and is more elegant than editing the init script:

PM2_HOME=/home/myuser/.pm2 pm2 startup ubuntu -u myuser

Unfortunately this command starts a pm2 God daemon before the init script is created, so I'm left with a running pm2 daemon with the wrong settings owned by root that breaks everything because it points at /root/.pm2, as above.

The next problem is that it's hard to stop this daemon nicely because it looks in the wrong place for its config because the init script is now in effect (i.e. service pm2-init.sh stop doesn't work), so I have to aggressively pkill -f PM2 to get rid of it before a simple service pm2-init.sh start starts it with the correct user, pointing at the correct .pm2 folder. Phew.

A minor issue - why not rename the init script to just pm2 when installing it, like nearly all other services do?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

psparago picture psparago  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

shaunwarman picture shaunwarman  路  3Comments

rangercyh picture rangercyh  路  4Comments

rajendar38 picture rajendar38  路  3Comments