Andy Nguyen
Andy Nguyen

Reputation: 1033

Does pm2 auto restart application after reboot by default?

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?

Upvotes: 67

Views: 102452

Answers (7)

Radadiya Harsh
Radadiya Harsh

Reputation: 11

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

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

Upvotes: 1

Shihab Uddin Shakil
Shihab Uddin Shakil

Reputation: 125

First of all I run below command

pm2 save

Then Run

 pm2 resurrect

Upvotes: -2

Rajib
Rajib

Reputation: 630

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/

Upvotes: 12

Pst4r8
Pst4r8

Reputation: 11

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

Upvotes: 1

Anil Daal
Anil Daal

Reputation: 21

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.

Upvotes: 2

gabriel gava
gabriel gava

Reputation: 81

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/

Upvotes: 8

pzaenger
pzaenger

Reputation: 11992

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

Upvotes: 84

Related Questions