The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

Troubleshoot why a PM2 process errors

posted on 8.6.2023 by Below Surface in "PM2"

Running

pm2 status

May output something like

id  │ name                │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
16  │ test-frontend       │ default     │ 0.35.3  │ fork    │ 0        │ 0      │ 16    │ errored   │ 0%       │ 0b       │ bel…     │ disabled

Let's find out why the process errored!

cd ~/.pm2/logs

If you have many logs in here already and no need for them, you could delete them all. Alternatively you can just delete the log for this process

sudo rm *.log // for deleting all .log files in that directory
sudo rm test-frontend.log // for deleting just the log for this process

Now, restart the PM2 process:

pm2 restart test-frontend 
// or
pm2 restart 16

Check if the error still persists:

pm2 status

If yet, stop it:

pm2 stop test-frontend
// or
pm2 stop 16

And check the logs.

cat test-frontend-error.log

In my case the output was:

Unknown or unexpected option: --no-autorestart

I remember that I just added the flag "--no-autorestart" to the pm2 start command a few days ago. It seems like I placed it wrong. So I fixed the command and ran the bash script which fires the pm2 start command again to be:

pm2 start npm --name test-frontend --no-autorestart -- start -- -p 3001

It is a Next.js frontend, started on the local port 3001 and "--no-autorestart" is a command for PM2 to no restart the process, if it fails.

Tags:

pm2
node process manager
linux
ubuntu

More posts of this category

Useful PM2 commands for Linux

PM2 is a well working process manager for the Node.js runtime

PM2

Disabling the auto restart of PM2 processes

Sometimes the server would run out of ressources, this is how i fixed it

PM2