Does pm2 auto restart application after reboot by default?
Asked Answered
M

7

62

I have an issue with my linux server and need to reboot, before that I run my node application with pm2 start server.js without any other config. Can it auto restart my app after reboot server?

Montero answered 6/2, 2020 at 12:43 Comment(0)
M
80

Not by default, but PM2 can do so using a startup script:

PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.

After generating your startup script (read also this comment), take a look at pm2 save:

Once you started all the applications you want to manage, you have to save the list you wanna respawn at machine reboot with:

pm2 save

Merous answered 6/2, 2020 at 12:48 Comment(3)
so in short: run pm2 startup and when all the right processes are running that you want to automatically restart at reboot save them with pm2 saveColtoncoltsfoot
@Ravindra See pm2-installer: pm2-installer is designed to automate installation of pm2 as a service, particularly on Windows, even in environments without Internet access.Merous
@Coltoncoltsfoot "pm2 startup" only prints a bash command that you need to copy and execute manually. After that, the processes you saved will be restarted on boot.Vaios
C
5

You can use this script before run pm2 save:

pm2 startup
[PM2] You have to run this command as root. Execute the following command:
      sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v14.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
      

https://pm2.keymetrics.io/docs/usage/startup/

Chenille answered 4/7, 2022 at 17:50 Comment(0)
A
4

Yes, you can. Suppose your pm2 is running having some processes. First, you need to save the processes.

pm2 save

Next, you need to run the pm2 in startup. So if system rebooted your pm2 automatically started with processes.

pm2 startup

Thats it! https://pm2.keymetrics.io/docs/usage/startup/

Andromede answered 4/8, 2023 at 16:27 Comment(0)
P
1

I used another alternative way on Ubuntu 20.04 and it worked!

Make sure you have saved the pm2 configuration (pm2 save)

First edit the crontab file

crontab -e

Then paste the following command

@reboot /usr/lib/node_modules/pm2/bin/pm2 resurrect && /usr/lib/node_modules/pm2/bin/pm2 start all
Primordial answered 19/7, 2023 at 23:36 Comment(0)
H
0

No, if you do not add a startup command then it will close after reboot so

you can use:

pm2 startup ubuntu

After this, it's always run after closing the server in the terminal.

Heterotypic answered 1/3, 2023 at 9:16 Comment(0)
A
0

pm2 start app.js --restart-delay=3000

https://pm2.keymetrics.io/docs/usage/restart-strategies/

Adelleadelpho answered 8/7 at 7:33 Comment(0)
D
-2

First of all I run below command

pm2 save

Then Run

 pm2 resurrect
Decode answered 6/10, 2022 at 8:2 Comment(2)
pm2 save, then pm2 resurrect on each (windows) startDionnedionysia
This will try to resurrect PM2 every minute, which doesn't seem like a perfect Idea.Broil

© 2022 - 2024 — McMap. All rights reserved.