24

I am using PM2 to run my node app as a service. My Node app is RAM Hungry so it consumes around 300 to 800mb of ram depending on traffic. My DigitalOcean droplet has 1GB RAM.

Sometimes out of the blue, my app disappears from running services and I see nothing when I give command pm2 list

As far as I know, pm2 should take care of such crashes itself and restart the app when ever such crash happens, but it doesn't restart it instead do anything.

I am now looking for some solution that can auto restart my app when it crashes, and I have to use pm2 to use it a service.

Saswata
  • 1,290
  • 2
  • 14
  • 28
Usman Tahir
  • 2,513
  • 4
  • 24
  • 38

2 Answers2

58

Can you check the logs if the machine rebooted or not?

You can use this command to see the last reboot time :

last | grep reboot

Or even the

uptime

Command to see for how long you are running.

If it does, that should be the problem. pm2 got a good solution management for that :

$ pm2 startup         # Detect init system, generate and configure pm2 boot on startup
$ pm2 save            # Save current process list
$ pm2 resurrect       # Restore previously saved processes
$ pm2 unstartup       # Disable and remove startup system

If your apps are running on ports that should be open with your firewalls, you need to do the same kind of saving with your firewall too. With Ubuntu I know there is a very nice package for that, but I am pretty sure you can find something equivalent on any system.

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
Alburkerk
  • 1,564
  • 13
  • 19
  • 1
    I just edited my answer with some example of linux command to see relevant informations about that. I don't know your distribution though, but I am pretty sure the information will be easy to find if my examples are not enought – Alburkerk Jul 31 '17 at 14:41
  • Ohh, I checked this, the machine was up for a long time, PM2 process just disappears from the list. Its Ubuntu 16.04 LTS – Usman Tahir Aug 01 '17 at 19:00
  • Can you try to launch your daemon then reboot your machine manually ? shutdown -r now should do the trick (don't forget the -r to make reboot and no simple shuttdown). Then see if your process disappeared (unstartup your process first if you already did what I said in my answer). – Alburkerk Aug 02 '17 at 07:43
  • @Alburkerk I am interested to know which package y are using for the firewall. – ans1genie Dec 05 '20 at 12:20
2

You only need to run three commands. First:

pm2 startup

Then copy paste and execute the command that it shows, which will be something like this:

sudo env PATH=$PATH:/home/user/.nvm/versions/node/v18.12.1/bin /home/user/.nvm/versions/node/v18.12.1/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user

And finally:

pm2 save

Doing this, the next time the system reboots, all pm2 process will be automatically started.

Victor
  • 893
  • 1
  • 10
  • 25