pm2 Startup not starting up on Ubuntu
Asked Answered
I

7

18

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?

Initiate answered 15/1, 2016 at 22:39 Comment(0)
O
28

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
Onwards answered 1/6, 2016 at 4:32 Comment(4)
This worked for me on Ubuntu 14.04.5 LTS. Readout from my initial stab at pm2 startup ubuntu told me to use pm2 startup upstart. Did that, now my app starts up on reboot.Noh
I am using Ubuntu Xenial 16.04. What worked for me was: pm2 save...pm2 startup upstart. Yeah!Comfit
@Noh it depends on the processes management which ubuntu uses, which depends on ubuntu versions. You may check case specifics here: pm2.keymetrics.io/docs/usage/startupHydrangea
if i manually stop an application pm2 stop 1, should it be restarting when the machine reboots if i've done these steps? i thought it would but it isn't.Corselet
C
6

The solution for me was restarting pm2 with systemctl: systemctl reload-or-restart pm2-root

When I setup my Ubuntu 18.04 server on the first time, I ran pm2 start app.js to start my app. Then when I tried to run pm2 startup + pm2 save to restart the app on boot, this seemed to be not working, as by running systemctl list-units, pm2 didn’t show up in the services list. Even though the app was running (pm2 list confirmed that). So I ran systemctl list-units -all, and pm2 showed as “inactive” and “dead”.

So I did:

  • systemctl status pm2-root (just to confirm that it was "inactive"/"dead")

  • systemctl reload-or-restart pm2-root (to restart pm2 through systemctl)

  • systemctl status pm2-root (to confirm that pm2 now was “active”/“running”)

  • Then I also ran systemctl enable pm2-root, to enable PM2 to run on startup (not sure if necessary)

  • And pm2 startup + pm2 save again (for the start on boot)

OBS.: I used pm2-root in my commands, as I was running pm2 with root user, but you should replace for your user (pm2-<USER>), if needed.

Conga answered 15/1, 2019 at 5:33 Comment(2)
same issue. Thank youAramen
Worked for me as wellGeneratrix
R
4

I think I solved the problem

I played with pm2, and found two working way for CentOS 7 (in your project folder index.js - main file):

  • 1

    sudo pm2 start index.js
    sudo pm2 save
    sudo pm2 startup centos
    
  • 2

    pm2 start index.js
    pm2 save
    pm2 startup centos
    '# and run the script generated in the previous code under sudo
    
Ronna answered 24/3, 2016 at 3:20 Comment(1)
I'll have to give this a shot!Initiate
V
1

Have you tried checking your boot logs on Ubuntu ( cat /var/log/boot.log )?

You probably see an error like:

Error: EACCES, permission denied '/home/<USER>/.pm2'

I haven't been able to solve this issue myself but at least this should point you in the right direction.

Vanna answered 5/2, 2016 at 10:36 Comment(3)
I checked that file and there is nothing in there :(Initiate
Could it be that the permissions on that folder are too strict? Just a guess.Savona
This is the solution to permission issue: github.com/Unitech/pm2/issues/1321#issuecomment-510348227Bezel
A
1

I know it's pretty old, but here is my version:

  1. Start processes with pm2: pm2 start app_name.js

  2. Save processes in the list: pm2 save

  3. Generate startup script: pm2 startup

Here we'd get the command list.

One of these commands highlighted with green must be implemented in the next step

  1. Activate script (command from the previous step): systemctl enable pm2-root

The reason I post this is because I think it is not obvious that this last command is required. But without it startup on reboot wont work.

Arbour answered 7/10, 2023 at 21:0 Comment(1)
Please read Why should I not upload images of code/data/errors? Instead, format code as a code block. The easiest way to do this is to paste the code as text directly into your question, then select it and click the code block button.Saltation
U
0

The got following codes in Ubuntu:

pm2 start main.js
pm2 save
pm2 startup ubuntu

Note that "pm2 save" will create a dump.pm2 file in /root/.pm2/dump.pm2 and this one will be used when your machine is restarted.

And if you don't know your operation system name just use:

pm2 startup

PM2 will detect your operating system automatically.

Uncover answered 17/9, 2020 at 3:28 Comment(0)
S
0

Solution is:‎

  1. Move to folder where you installed the PM2 (such as /home/ubuntu). There will be ‎a folder ‘.pm2 ‎.‎

  2. Run ‘PM2 startup’ to generate an active startup script.‎

  3. Run ‘PM2 save’ to freeze a process list for automatic respawn.’.‎

Now all the processes will auto start on roboot.

Sutphin answered 5/11, 2021 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.