PM2 Startup script, always keep processes running

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.
To generate a startup script for PM2, we can run this command.
pm2 startup
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
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.
Make sure you have all the PM2 processes running you want on startup.
Now we can run:
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.
pm2 resurrect
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter