Pm2: Can't run pm2 as another user

Created on 21 Mar 2016  ยท  9Comments  ยท  Source: Unitech/pm2

hey,

I'm trying to run pm2 as another user with this command:
sudo -u scripts bash -c 'pm2 restart 2'
but this still runs it as the logged in user.

Pending Release

Most helpful comment

Published on PM2 2.6.0:

$ npm install pm2@latest -g
$ pm2 update

All 9 comments

As long as the user you want to run pm2 from has the pm2 binary in it's path it should work. Note that every pm2 instance has it's own memory daemon. This means that you should first start the pm2 instance from that user so that the in-memory process user is the correct one.

Take this screenshot for example:
screenshot from 2016-03-23 11 57 18

Note that I'm sourcing my .zshrc file because www-data has a /usr/sbin/nologin shell.

What I did for this to work:

  1. Install nodejs through nvm from the www-data user. It now has it's own nodejs binary, with it's own nodejs version
  2. Install pm2 from the www-data user, you can see on the screenshot that both users (me and www-data) are using different pm2 versions (and different nodejs versions)!

Be careful when you issue start commands, if a server is running from www-data you won't be able to start it from another user without changing it's listening port. It can also be disturbing because the pm2 list will only show processes managed by the user issuing the command (better for security).

_Note that you should avoid using multiple nodejs versions on the same machine (or multiple pm2 versions). I did this for example purpose. It might give you run errors when trying to run compiled source from one version with another. Also it might be confusing if someone doesn't know that both users aren't using the same version._

ps output:

www-data  2398  2389  0 12:01 ?        00:00:00 node /var/www/helloworld.js
abluchet 29981 29798  0 11:56 ?        00:00:00 node /home/abluchet/forks/pm2/test/fixtures/server.js

Hm, so I have to install nodejs and pm2 in the user who owns the scripts and in the user which will run the script as the other user?

This is prefered to avoid errors. If the user has correct ownerships on ~/.pm2 of the other user you could have only one installation. I haven't tested this but it's not officially supported.

I can run pm2 show fine on the "rundeck" user but I still can't run sudo -u scripts bash -c 'pm2 show'

I get this error if I run it:

Error: connect EACCES /home/rundeck/.pm2/rpc.sock
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)

So it still runs it as rundeck not scripts.

Add the --hp option to specify the user home path or it'll take the current user $HOME.

I'm not aware of a --hp option for sudo.
How do you mean it?

Hi ๐Ÿ˜„

The problem I am facing today also is similar (or identical)

jenkins@ip-172-xx-xx-xxx:~$ pm2 reload all -u ubuntu --hp /home/ubuntu
Use --update-env to update environment variables
[PM2][WARN] No process found

jenkins@ip-172-xx-xx-xxx:~$ exit
ubuntu@ip-172-xx-xx-xxx:~$ pm2 reload all
Use --update-env to update environment variables
[PM2] Applying action reloadProcessId on app [all](ids: 0,1,2,3)
[PM2] [app](0) โœ“
[PM2] [app](1) โœ“
[PM2] [app](3) โœ“
[PM2] [app](2) โœ“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App name โ”‚ id โ”‚ mode    โ”‚ pid  โ”‚ status โ”‚ restart โ”‚ uptime โ”‚ cpu  โ”‚ mem       โ”‚ watching โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ app      โ”‚ 0  โ”‚ cluster โ”‚ 7157 โ”‚ online โ”‚ 7       โ”‚ 1s     โ”‚ 55%  โ”‚ 89.6 MB   โ”‚ disabled โ”‚
โ”‚ app      โ”‚ 1  โ”‚ cluster โ”‚ 7158 โ”‚ online โ”‚ 7       โ”‚ 1s     โ”‚ 55%  โ”‚ 89.4 MB   โ”‚ disabled โ”‚
โ”‚ app      โ”‚ 2  โ”‚ cluster โ”‚ 7181 โ”‚ online โ”‚ 7       โ”‚ 1s     โ”‚ 104% โ”‚ 85.8 MB   โ”‚ disabled โ”‚
โ”‚ app      โ”‚ 3  โ”‚ cluster โ”‚ 7187 โ”‚ online โ”‚ 7       โ”‚ 0s     โ”‚ 104% โ”‚ 88.5 MB   โ”‚ disabled โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
 Use `pm2 show <id|name>` to get more details about an app

Can you please provide an approach so that jenkins user (not a sudo-er, but can gain root access via a script by visudo configuration) can perform actions on pm2 running in other user's context.

Thanks in advance ๐Ÿ˜„

Feature added in upcoming PM2. To try it:

(Make sure pm2 is run as root to manipulate uid and gid)

$ pm2 save
$ pm2 kill
$ npm install Unitech/pm2#development -g
$ sudo pm2 resurrect
$ sudo pm2 start app --uid <uid>
# Now pm2 ls display the user running the script
$ pm2 ls

Feedback is welcome

Published on PM2 2.6.0:

$ npm install pm2@latest -g
$ pm2 update
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexpts picture alexpts  ยท  3Comments

rajendar38 picture rajendar38  ยท  3Comments

waygee picture waygee  ยท  4Comments

cchan picture cchan  ยท  3Comments

FujiHaruka picture FujiHaruka  ยท  3Comments