pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

PM2 Pid file is not being create.

Open HamidShaikh1499 opened this issue 9 months ago • 2 comments

I installed node and pm2(globally) as root user.

I setup all my project as non sudo user and setup ecosystem files for start applications using pm2

When i run pm2 startup. it provides command that command i run as root user. and then pm2 save.

After restart server, then pm2 is not able to restart all applications.

I checked pm-user.service is failing because pm2.pid file not found

I tried many times but PM2.PID is not creating So pm-nonuser.service is failing

Any solution??

HamidShaikh1499 avatar Mar 21 '25 04:03 HamidShaikh1499

?

HamidShaikh1499 avatar Mar 24 '25 04:03 HamidShaikh1499

Reason:-

Yes, this is a common PM2 + systemd + multiple user context issue.

When you install Node.js and PM2 as root, but run your apps and ecosystem setup as a non-root user, PM2 gets confused during pm2 startup because it mixes context between the root and non-root user environments.

Let’s fix it step-by-step so that your PM2 setup works reliably across reboots for the non-root user.

Step-by-Step Solution

  1. Clean Existing PM2 Root Setup (Optional But Recommended) This avoids conflict between root and non-root PM2 environments.

Run this commands as root:

pm2 unstartup
pm2 kill
  1. Switch to Your Non-root User Let’s assume your non-root user is devuser. Switch if you're not already:
su - devuser

Or if you're logged in directly as devuser, skip this step.

  1. Install PM2 Locally (Non-root) Make sure pm2 is installed in this user’s environment.
npm install -g pm2
  1. Run PM2 Startup for the Current User This is crucial: Run pm2 startup as the non-root user.
pm2 startup

It will show a command like this:

sudo env PATH=$PATH:/home/devuser/.nvm/versions/node/vXX/bin pm2 startup systemd -u devuser --hp /home/devuser

Copy and paste that command and run it as root (with sudo).

sudo env PATH=$PATH:/home/devuser/.nvm/versions/node/vXX/bin pm2 startup systemd -u devuser --hp /home/devuser

This sets up a proper pm2-devuser.service with the correct environment.

  1. Save the Current PM2 Process ListStill as devuser:
pm2 save

This saves the process list that should be auto-started on reboot.

  1. Reboot and Test
sudo reboot

After reboot, log in as devuser and run:

pm2 ls

Your apps should be running automatically.

rohankamble103 avatar Apr 09 '25 05:04 rohankamble103