How to run pm2 so other server users are able to access the process?

Nik Sumeiko picture Nik Sumeiko · Aug 24, 2015 · Viewed 29k times · Source

When I start my Nodejs app with pm2, other server users are not able to access the process.

Even if I start pm2 from a custom directory (not current user's ~/, what pm2 is using by default):

HOME=/var/www pm2 start app.js

Directory is accessible by any user (comparing to ~/, but there's still no way other server user is able to access the process.

When other server user does pm2 list, it shows him 0 processes are running – but there are (started by another user). And when other user tries HOME=/var/www pm2 list, CLI throws an error:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: connect EACCES
    at errnoException (net.js:905:11)
    at Object.afterConnect [as oncomplete] (net.js:896:19)

So I am wondering how to make sure users are able to access pm2 processes run by other server users? Or it shall be approached differently?


I am wondering why every server user is able to make git pull to deploy latest source code from a Git repository, but can't restart pm2 process afterwards? Only the user that started pm2 process is able to restart it… Weird.

Answer

vinayp picture vinayp · Oct 7, 2015

Here's how we bypassed this.

Just create a group

  • Create a new group pm2 or whatever name works for you

    $ groupadd pm2

  • Change the /var/www/ folder group owner to group pm2

    $ chgrp -R pm2 /var/www

  • Add the other user, let's say bob, to pm2

    $ usermod -aG pm2 bob

Now bob can run pm2 commands by changing $HOME to /var/www

$ env HOME=/var/www pm2 list

Or (better still) create an alias as @jcollum suggested

$ alias pm2='env HOME=/var/www pm2'