pm2 Startup not starting up on Ubuntu

Rastalamm picture Rastalamm · Jan 15, 2016 · Viewed 11.6k times · Source

I am having difficulty getting pm2 to restart (itself and two node/express files, app.js & app2.js) on a server re-boot.

Below is the processes I have tried:

pm2 startup
pm2 start app.js
pm2 start app2.js
pm2 startup ubuntu (also tried systemd and with/without -u username)
pm2 save

I ran the above commands in every possible combination and nothing worked. I tried running as root and it did not work either.

My ~/.pm2/dump.pm2 file contains information so I am not sure where else to look.

I have attempted to modify my /etc/init.d/pm2-init.sh file according to this issue but it did not help.

My Setup:
Digital Ocean Server
Ubuntu 15.10
Node v5.4.1
PM2 v 1.0.0

Other references I tried..
http://pm2.keymetrics.io/docs/usage/startup/
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
https://gist.github.com/leommoore/5998406
https://www.terlici.com/2015/06/20/running-node-forever.html
https://serversforhackers.com/node-process-management-with-pm2 http://nodered.org/docs/getting-started/running.html#starting-node-red-on-boot
https://github.com/Unitech/pm2/issues/1316

So far, each time I reboot the server, pm2 fails to startup automatically (unless I need to wait a few minutes? - nginx restarts instantly).

Can someone help me out with this? What is the order in which the commands should be run? Do I need to modify any additional files?

Answer

Michael Rechenberg picture Michael Rechenberg · Jun 1, 2016

You have to save your node app using

 pm2 start [app_name]
 pm2 save

Then do:

 pm2 startup [operation system]

This will create a dump.pm2 file that pm2 needs to run your app on reboot.

Operation system:

  • systemd: Ubuntu >= 16, CentOS >= 7, Arch, Debian >= 7
  • upstart: Ubuntu <= 14
  • launchd: Darwin, MacOSx
  • openrc: Gentoo Linux, Arch Linux
  • rcd: FreeBSD
  • systemv: Centos 6, Amazon Linux