Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 735
Nik Sumeiko Avatar asked Aug 24 '15 09:08

Nik Sumeiko


People also ask

Does pm2 need to be installed globally?

yes, you need to install it globally.

How do I show pm2 process?

To get an overview of the processes currently managed by PM2, you can print out a comprehensive list using the command line utility. The output shows basic information about the running processes like app name and id, the mode ( fork or cluster ), status, uptime, memory footprint, etc.

What is pm2 Process Manager?

PM2 is a Node. js process manager that comes with a built-in load balancer. It helps facilitate production deployments and enables you to keep running applications alive indefinitely (even when accidents occur).


1 Answers

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'

like image 114
vinayp Avatar answered Sep 19 '22 21:09

vinayp