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?
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
pm2-installer
: pm2-installer
is designed to automate installation of pm2 as a service, particularly on Windows, even in environments without Internet access. –
Merous 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>
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
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
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.
pm2 start app.js --restart-delay=3000
First of all I run below command
pm2 save
Then Run
pm2 resurrect
pm2 save
, then pm2 resurrect
on each (windows) start –
Dionnedionysia © 2022 - 2024 — McMap. All rights reserved.
pm2 startup
and when all the right processes are running that you want to automatically restart at reboot save them withpm2 save
– Coltoncoltsfoot