Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.24k stars 2.61k forks source link

Show processes for all users at once? #1661

Open shukriadams opened 8 years ago

shukriadams commented 8 years ago

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!

JCMais commented 8 years ago

Would love some information if that is planed or not.

scippio commented 7 years ago

+1

soyuka commented 7 years ago

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).

mohan-shyam commented 6 years ago

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.

Clemens-E commented 5 years ago

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

k1tzu commented 5 years ago

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?

dmackinn commented 4 years ago

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.

harrisoff commented 4 years ago

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.

cjohnweb commented 4 years ago

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
stale[bot] commented 4 years ago

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.

AminNadimi commented 4 years ago

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...

swapnil0545 commented 4 years ago

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

stale[bot] commented 4 years ago

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] commented 4 years ago

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.

cjohnweb commented 3 years ago

@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.

ScarVite commented 3 years ago

nothing new?

BuildersRejected commented 3 years ago

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.

LenhardErwan commented 3 years ago

nothing new?

leader86 commented 3 years ago

+1

sshkpp commented 3 years ago

+1

DanielHougaard commented 2 years ago

We need this so bad.

tasaif commented 2 years ago

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
lctcharly commented 2 years ago

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 commented 2 years ago

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.

ramaces commented 1 year ago

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`