How to change default directory for invocation of pm2
Asked Answered
pm2
I

1

5

I've got a nodejs server that is being managed to pm2, but it is running as a different user (www-data, via su). I need to start a second server run as my own user id, but when I try to start it, I get an access error as ~/.pm2 is owned by www-data, not myself. I tried using the --log option to pm2, but that doesn't let me set the global pm2 directory, just the log file.

How can I specify a different directory for pm2 to use?

Imaginal answered 4/3, 2021 at 19:9 Comment(0)
F
6

The short answer is to set a different home directory for pm2...

$ export PM2_HOME=/your/pm2/home
$ <your pm2 commands>

Of course, whatever directory you wish to put the .pm2 directory into will have to be writable by the user as which you are invoking pm2.

The long answer would be to shut down the running server run by www-data and remove the ~/.pm2 directory from your home directory.

Then, when you start the pm2 process as the www-data user do it like this

$ su -c "PM2_HOME=/path/writable/by/www-data/user <pm2 commands>" www-data

Now, the existing pm2 server should be running as www-data with its runtime files in whatever directory you set up for the www-data user.

Which means you can now use your own home directory (by default) when you run any pm2 commands as you, without having to configure any PM2_HOME variable.

Fallacious answered 27/12, 2022 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.