7

PM2 Startup script, always keep processes running

 2 years ago
source link: https://dev.to/dailydevtips1/pm2-startup-script-always-keep-processes-running-40pa
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for PM2 Startup script, always keep processes running

PM2 Startup script, always keep processes running

Jun 22 Originally published at daily-dev-tips.com

・2 min read

We had a short introduction into PM2, a process manager for Node scripts. However, there is one big thing we didn't cover yet, and that's startup scripts.

These are PM2 scripts we can generate, so the processes running will restart themselves on an unexpected reboot of your server.

We had this issue before a server rebooted for software updates, but our node-driven websites would not start automatically.
This is where PM2 startup scripts shine.

Generating a PM2 startup script

To generate a startup script for PM2, we can run this command.

pm2 startup
Enter fullscreen modeExit fullscreen mode

Running this will return the following:

[PM2]
To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/Users/chrisbongers/.nvm/versions/node/v14.15.4/bin /Users/chrisbongers/.nvm/versions/node/v14.15.4/lib/node_modules/pm2/bin/pm2 startup launchd -u chrisbongers --hp /Users/chrisbongers
Enter fullscreen modeExit fullscreen mode

When you copy that line, you'll be done with this part!

If you ever need to redo the startup script fully, you can run pm2 startup and thenpm2 startup` again.

Saving all running PM2 processes

Make sure you have all the PM2 processes running you want on startup.

Now we can run:

bash
pm2 save

This will create a dump.pm2, which will auto startup our scripts.

Now, whenever your server reboots, your processes will automatically restart as well 🤗.

If you ever need to reboot all processes manually, you can also execute the following command.

bash
pm2 resurrect

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK