Running Node-Red as an AutoStart Server with PM2 on Startup

Posted February 20, 2024
Running a Node-Red as an AutoStart Server with PM2

This guide teaches you to run a Node-Red as an AutoStart Server with PM2 (Process Manager 2). This setup should automatically start and manage Node-RED in the background on boot.

PM2 will manage, monitor, and keep your Node-RED running in the background as a daemon. This means you are enabling AutoStart functionality to Node-RED whenever the system boots up.

If you are using Raspberry, check this How to Perfectly AutoStart Node-Red on Raspberry Pi Boot up guide.

Let’s dive in and learn the steps of Running a Node-Red as an AutoStart Server with PM2. This setup should start Node-Red on boot for Raspberry, Linux, and Ubuntu.

Step 1: Requirements to AutoStart Node-Red with PM2

Before diving into this guide, you must have a Node-Red running on your system. This guide only set up AutoStart Node-Red with PM2. Check the following guides to get Node-Red server ready first:

Ready. Check the following steps to install PM2 with NPM.

Step 2: Installing Node-RED and PM2

You already have installed Node-RED. This means you have Node.js and NPM. Now go ahead and install PM2 globally by running the following commands:

sudo npm install -g pm2

Step 3: Running Node-RED with PM2

Initially, you would use the node-red command to run Node-Red. With PM2 the command will change. Use the following command:

pm2 start `which node-red` -- -v

This should start the start Node-RED server with PM2 as follows:

Running Node-RED with PM2

If you have limited resources --max-old-space-size=128 flag to reduce memory usage:

pm2 start `which node-red` -- --max-old-space-size=128

Step 4: AutoStart Node-RED with PM2

Node-RED is running with PM2. However, you need to add the node-red process list to ensure Node-RED will be started automatically on system boot. This is where AutoStart comes in. Use the following command:

pm2 save

AutoStart Node-RED with PM2

Next, configure the PM2 Startup Script. This will be the startup scripts to start PM2 and its managed processes automatically on system boot:

pm2 startup

AutoStart Node-RED with PM2

  • Copy your above Start script and run it (Check the above image To set the startup script. copy/paste the following command)

AutoStart Node-RED with PM2

Step 5: Verifying Node-RED AutoStart with PM2

To Verifying that Node-RED AutoStart is running and managed by PM2, list all running processes with the following command:

pm2 list

Verifying Node-RED AutoStart with PM2

Now, you should see node-red in the process list. The same should apply if you monitor PM2 with the following command:

pm2 monit

Verifying Node-RED AutoStart with PM2

Step 6: How to Manage Node-RED with PM2

The last step is to be able to manage the running node-red process list with PM2 Use the following commands:

  • pm2 stop node-red: Stop Node-RED.
  • pm2 restart node-red: Restart Node-RED.
  • pm2 delete node-red: Remove Node-RED from PM2’s process list (won’t uninstall Node-RED).
  • pm2 logs node-red: View Node-RED logs.

Conclusion

Reboot your system and make sure PM2 is working and able to AutoStart your Node-RED server. For further details check this guide and learn How to Add Secure SSL HTTPS Certificate to Node RED.

Running Node-Red as an AutoStart Server with PM2 on Startup

Written By:

Joseph Chege