pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Show processes for all users at once?

Open shukriadams opened this issue 10 years ago • 31 comments

On my raspberry pi, I have an init.d script that I use to start my pm2 processes :

start)
su root -c "...pm2 start ...somescript.js --name someName" 

The scripts I'm starting require root, so "su pi" won't work. Starting scripts with pm2 this way works fine, but that I can't see the processes started as user:root when I'm logged in as user:pi and run "pm2 list". I can however see them when I do

sudo pm2 list

and then I can start, stop etc them as normal, as long as I prefix all pm2 commands with "sudo".

I can also start/stop processes directly from the command line with su root (and password entry) like :

su root -c "...pm2 stop someName" 

Is there any chance of us getting a unified list of all pm2 processes on the current system regardless of which user started it? Or at least have "sudo pm2 list" return all processes? I wouldn't class this as a bug, it would just be that much more convenient.

Also, pm2 is a godsend, thanks for making nodejs server coding that much easier to manage!

shukriadams avatar Oct 01 '15 18:10 shukriadams

Would love some information if that is planed or not.

JCMais avatar Jul 31 '16 20:07 JCMais

+1

scippio avatar Nov 03 '16 14:11 scippio

IMO this would be a security issue.

Root might be able to access user's PM2 but not the other way around. The problem now is to find which users are having a pm2 daemon (I might have some ideas about this, I'll keep you tuned).

soyuka avatar Nov 03 '16 14:11 soyuka

I have a similar issue, where our developers (not with them anymore) had started our process, and we're now not able to look at them. So, how do I go about it? Is there a way I could login as root, and delete the currently running process, and restart from my user account?

This is real priority and important. Thanks in advance.

mohan-shyam avatar Apr 14 '18 11:04 mohan-shyam

@soyuka what is the status of that "feature". I need it to manage the process of a few users (as root).

Clemens-E avatar Oct 19 '18 20:10 Clemens-E

Seriously there're no way to list all pm2 managed processes as root? How do you suppose to manage a range of production services then?

k1tzu avatar Oct 21 '18 11:10 k1tzu

I would also love to see this feature added. The node applications we're running with pm2 need to run as a non-interactive user (www) but users with login accounts need to be able to see the processes. It would be nice to see all processes without having to use su to run pm2 as another user.

dmackinn avatar Oct 23 '19 16:10 dmackinn

I started a process with sudo in jenkins shell, and it worked. But after that in the ssh terminal, I could not find that proccess using sudo pm2 status.

harrisoff avatar Jan 19 '20 08:01 harrisoff

Hope this might be helpful in the mean time...

# ps aux | grep pm2
cj      1627  3.5 20.3 1239240 101496 ?      Ssl  14:54   0:13 PM2 v3.3.1: God Daemon (/home/cj/.pm2)
root      2903  0.2  7.8 930520 39244 ?        Ssl  14:54   0:00 PM2 v3.3.1: God Daemon (/root/.pm2)
root      2927  0.1  7.6 1188440 38272 ?       Ssl  14:55   0:00 PM2 Agent v0.5.23: (/root/.pm2)
root     11596  0.0  0.1  12940   972 pts/0    S+   15:00   0:00 grep --color=auto pm2

cjohnweb avatar Jan 20 '20 23:01 cjohnweb

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 23 '20 21:05 stale[bot]

The thread is from 2015 and now it's 2020 Why don't have the feature YET?! :( Only one of those 2.2k forks was required to do this, Just one...

AminNadimi avatar May 27 '20 07:05 AminNadimi

I would like to have this feature or insights on how we can find such things. @k1tzu @cjohnweb In that code you shared, which app is actually running the process? out of 4

swapnil0545 avatar Jun 03 '20 12:06 swapnil0545

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 03 '20 12:07 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 16 '20 07:08 stale[bot]

@k1tzu @cjohnweb In that code you shared, which app is actually running the process? out of 4

@swapnil0545 One of the first 2...not sure which though, need to check.

cjohnweb avatar Aug 24 '20 05:08 cjohnweb

nothing new?

ScarVite avatar Sep 19 '20 16:09 ScarVite

I am also noticing this as an issue. I want users to log in remotely and start their scripts with pm2. This is doable, but I want to be able to log in as a "admin" user and manage all processes. There is also an option to start the process "as a user" sudo pm2 start index -u userperson1 but i noticed that once i was logged in to that user (userperson1) I was unable to see that process. Its seperate as a process for 'sudo'. Since the -u option cant be used without sudo, no other user can manage it without sudo permissions including the user specified with the '-u' option.

BuildersRejected avatar Dec 30 '20 03:12 BuildersRejected

nothing new?

LenhardErwan avatar Jun 17 '21 11:06 LenhardErwan

+1

leader86 avatar Jul 02 '21 14:07 leader86

+1

sshkpp avatar Aug 15 '21 15:08 sshkpp

We need this so bad.

varonix0 avatar Aug 26 '21 21:08 varonix0

After reviewing the source code, here is a quick and dirty terrible workaround.

#!/usr/bin/env bash

function get_nodeapps {
  pm_users=`ps aux | grep "PM2.*God Daemon" | grep -v grep | cut -d ' ' -f 1 | uniq`
  for pm_user in $pm_users; do
    apps=`sudo su - $pm_user -c "pm2 jlist"  | jq '.[] | .name,.pm2_env.pm_exec_path,.pm2_env.pm_out_log_path,.pm2_env.pm_err_log_path'`
    if [ "$apps" == "" ]; then
      continue
    fi  
    echo "User: $pm_user"
    echo "$apps" | {
      while IFS= read -r name
      do
        read -r path
        read -r log_path
        read -r err_path
        echo -e "\tname: $name"
        echo -e "\tpath: $path"
        echo -e "\tlog_path: $log_path"
        echo -e "\terr_path: $err_path"
        echo
      done
    }   
    break
  done
}

get_nodeapps

tasaif avatar Dec 10 '21 22:12 tasaif

We are planning to include PM2 to manage the process in prod, and we are actually getting a couple of conflicts trying to get the service running from other users(testing in dev)...

Here a small-systemd service-workaround, which we are using to start, delete or reload a PM2 from a service, but it's unique.

/etc/systemd/system/pm2.service

[Unit]
Description=App Nodejs Service

[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/var/www/apps/[application_folder]

ExecStart=pm2 start shared/ecosystem.config.js
ExecReload=pm2 reload shared/ecosystem.config.js
ExecStop=pm2 kill shared/ecosystem.config.js

[Install]
WantedBy=multi-user.target

The plan here is to execute the following command from jenkins-DEPLOY to reload or to start the application when the folders and symlinks are already created.

but even with it, we are not able to get the PM2 log/status/monit executed from the service :(

Sorry if I'm missing something here, this is my first time commenting on a post :), but I'm facing the same cases.

lctcharly avatar Jan 27 '22 00:01 lctcharly

I found a proper solution for it!

And to be honest it's too really easy, after investigating across the forums I found a couple of ideas about what other users are doing.

First of all, pm2 daemon works under a specific folder, which will contain all the pid and the process running in the environment.

By default, PM2 is using the /root/.pm2 folder to locate all the required files and folders to maintain the process running. That folder is defined only for the root user.

But when we execute the command pm2 start ecosystem.config.js, automatically use our user home folder.

Example: I've a Ubuntu user called {CharlieDev} from { content } users, when I execute the command { $ pm2 start ecosystem.config.js }, It's generating a setup under my rights located in this folder { /home/CharlieDev/.pm2 } which means only CharlieDev is able to start, reload, watch the process and delete it.

First of all we are using a service to initialize the app process.

[Unit]
Description=Image resizer nodejs service

[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/var/www/apps/{app_name}/current/
Environment=PM2_HOME=/var/bin/pm2/

ExecStart=pm2 star
ExecReload=pm2 reload
ExecStop=pm2 kill

[Install]
WantedBy=multi-user.target

In this service we are defining WorkingDirectory, which contains the ecosystem.config.js file and all the projects (Looks like this property is similar to cd) and Environment, which will contain the custom folder containing the pm2 initialization.

Before continue with the next steps(#rights), we need execute the service, I named like pm2Service _

/etc/systemd/system/pm2.service

_ sudo service pm2 start executes the command in order to initialize and create the files and folder inside the /var/bin/pm2/, which is the new folder visible for all the content users.

(#rights) that folder contain the following rights;

$ chown content:content /var/bin/pm2 -R
$ chmod g+s /var/bin/pm2

At this point, we complete the main point, all the { content } users will be able to see the pm2 service executed. but... we need execute the following command each time:

PM2_HOME=/var/bin/pm2 pm2 ls

This is a small summary regarding the work that I made for dev, stage to release to prod in a couple of weeks.

We are planning to create load testing to see the behavior and the outputs, I will post my updates here.

lctcharly avatar Feb 02 '22 05:02 lctcharly

Hi @lctcharly

Thx for your code but there are some errors. i tried to fix them first of all when username is longer than 8 charachters it doenst work. second you added a break at to end of code inside for lus so iterate shouldn't work this way.

`#!/usr/bin/env bash

function get_nodeapps { pm_users=ps axo user:50,comm:50 | grep "PM2.*God" | grep -v grep | cut -d ' ' -f 1 | uniq for pm_user in $pm_users; do if [ "$pm_user" == "root" ]; then apps=pm2 jlist | jq '.[] | .name,.pm2_env.pm_exec_path,.pm2_env.pm_out_log_path,.pm2_env.pm_err_log_path' else apps=sudo su - $pm_user -c "pm2 jlist" | jq '.[] | .name,.pm2_env.pm_exec_path,.pm2_env.pm_out_log_path,.pm2_env.pm_err_log_path' fi

if [ "$apps" == "" ]; then
    echo "$pm_user"
    continue
fi
echo "User: $pm_user"
echo "$apps" | {
  while IFS= read -r name
  do
    read -r path
    read -r log_path
    read -r err_path
    echo -e "\tname: $name"
    echo -e "\tpath: $path"
    echo -e "\tlog_path: $log_path"
    echo -e "\terr_path: $err_path"
    echo
  done
}

done }

get_nodeapps`

ramaces avatar Apr 07 '23 12:04 ramaces

2025 and still not here :(

finnjosten avatar May 30 '25 08:05 finnjosten

hahaha three years 😮

to be honest we fixed this problem including the pm2 home: PM2_HOME=/our/location/files/.pm2 pm2 status

we create a "public" folder /our/location/files/.pm2, so all the members can have access to the process.

All the members that wants to get the pm2 status, stop or restart the process, can just include the pm2 home where the pm2 files are located, and everyone can have access to this process.

Since in my experience, this pipes or process id are located there, so when pm2 ran using this pm2 home folder/location, this is able to detect of what process you want to get information.

lctcharly avatar May 30 '25 18:05 lctcharly

hahaha three years 😮

to be honest we fixed this problem, including the pm2 home: PM2_HOME=/our/location/files/.pm2 pm2 status we you create a "public" folder /our/location/files/. so all the members can have access to the process.

All the members that wants to get the pm2 status or something, can just inlucde the pm2 home withing the pm2 files that command generated.

That's a great solution actually

dmackinn avatar May 30 '25 19:05 dmackinn

hahaha three years 😮 to be honest we fixed this problem, including the pm2 home: PM2_HOME=/our/location/files/.pm2 pm2 status we you create a "public" folder /our/location/files/. so all the members can have access to the process. All the members that wants to get the pm2 status or something, can just inlucde the pm2 home withing the pm2 files that command generated.

That's a great solution actually

Yes, this is what helped us at that moment. We had 12 VMs, and we wanted Jenkins to initialize or restart the service. So, we passed this new folder location to Jenkins(Where the .pm2 files are located), allowing it to initialize, restart, or create the process and from our end (as non-Jenkins users), we can then use that pm2_home to specify where we want to see the process running.

we are now using Docker and K8s, but pm2 was our process manager by 3 years.

lctcharly avatar May 30 '25 21:05 lctcharly

All good ideas i just created a wapper pmc2 that just runs pm2 as sudo so they get all created by one user. Best solution props not but still not suuper far into linux and all and this works more then good enough for me :)

finnjosten avatar May 31 '25 06:05 finnjosten