How to run multiple instances of Node-RED with PM2?

4,060 views
Skip to first unread message

Cory Guynn

unread,
Apr 5, 2016, 10:56:26 AM4/5/16
to Node-RED
Hi,

My Node-RED flows have gotten too large, so I have split them up into different flow files. I will then run each separately, specifying the flow file and port number.

It works if I manually run them but PM2 does not work with multiple instances of Node-RED.

I've tried to replicate this "micro-service" approach, but no luck.



//process.json (for PM2)

[
{
  "script"    : "/usr/local/bin/node-red -p 1880",
  "name"      : "node-red default"
},
{
  "script"    : "/usr/local/bin/node-red iol.json -p 1881",
  "name"      : "node-red IoL Master"
}, 
{
  "script"    : "/usr/local/bin/node-red iol.json -p 1882",
  "name"      : "node-red weather"
},
{
  "script"    : "/usr/local/bin/node-red iol.json -p 1883",
  "name"      : "node-red train schedule"
},
]


Any ideas or a better way to accomplish this?

Thanks,
Cory

Nathanaël Lécaudé

unread,
Apr 5, 2016, 11:42:09 AM4/5/16
to Node-RED
What error does pm2 output ?

Cory Guynn

unread,
Apr 5, 2016, 12:20:41 PM4/5/16
to Node-RED
When trying to run manually, not with the pm2 process.json file, it seems like it doesn't like multiple instances of node-red. Each time I run pm2 for a new node-red instance, it names it the same and naturally fails. 

If I run with the process.json file, it is expecting a JS file in the script section as opposed to the "node-red" launcher.

[PM2][ERROR] script not found : /usr/local/bin/node-red iol.json -p 1881
script not found : /usr/local/bin/node-red iol.json -p 1881

To remove the process.json method, I have just created a simple script to individually start each Node-RED instance. But this fails because PM2 thinks of them as the same application, even though they are running different flow files and ports.



my manual startup script

pm2 start /usr/local/bin/node-red -x --name "node-red default" -- -p 1880
pm2 start /usr/local/bin/node-red -x --name "node-red IoL master" -- iol.json -p 1881 
pm2 start /usr/local/bin/node-red -x --name "node-red weather station" -- weather.json -p 1882
pm2 start /usr/local/bin/node-red -x --name "node-red train schedule" -- trainschedule.json -p 1883
pm2 save
pm2 startup


dexter@Minty ~ $ sudo ./node-red-pm2.sh 
[PM2][ERROR] Script already launched, add -f option to force re-execution
┌──────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name         │ id │ mode │ pid   │ status │ restart │ uptime │ memory       │ watching │
├──────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ node-red default │ 0  │ fork │ 29198 │ online │ 1       │ 70s    │ 59.750 MB    │ disabled │
│ node-red default │ 1  │ fork │ 29205 │ online │ 57      │ 70s    │ 110.156 MB   │ disabled │
│ node-red default │ 2  │ fork │ 29878 │ online │ 2892    │ 0s     │ 12.324 MB    │ disabled │
└──────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[PM2][ERROR] Script already launched, add -f option to force re-execution
┌──────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name         │ id │ mode │ pid   │ status │ restart │ uptime │ memory       │ watching │
├──────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ node-red default │ 0  │ fork │ 29198 │ online │ 1       │ 71s    │ 59.750 MB    │ disabled │
│ node-red default │ 1  │ fork │ 29205 │ online │ 57      │ 71s    │ 110.156 MB   │ disabled │
│ node-red default │ 2  │ fork │ 29878 │ online │ 2892    │ 0s     │ 36.039 MB    │ disabled │
└──────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[PM2][ERROR] Script already launched, add -f option to force re-execution
┌──────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name         │ id │ mode │ pid   │ status │ restart │ uptime │ memory       │ watching │
├──────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ node-red default │ 0  │ fork │ 29198 │ online │ 1       │ 71s    │ 59.750 MB    │ disabled │
│ node-red default │ 1  │ fork │ 29205 │ online │ 57      │ 71s    │ 110.156 MB   │ disabled │
│ node-red default │ 2  │ fork │ 29878 │ online │ 2892    │ 0s     │ 54.109 MB    │ disabled │
└──────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[PM2][ERROR] Script already launched, add -f option to force re-execution
┌──────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name         │ id │ mode │ pid   │ status │ restart │ uptime │ memory       │ watching │
├──────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ node-red default │ 0  │ fork │ 29198 │ online │ 1       │ 71s    │ 59.750 MB    │ disabled │
│ node-red default │ 1  │ fork │ 29205 │ online │ 57      │ 71s    │ 110.156 MB   │ disabled │
│ node-red default │ 2  │ fork │ 29878 │ online │ 2892    │ 0s     │ 75.977 MB    │ disabled │
└──────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴──────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
[PM2] Saving current process list...
[PM2] Successfully saved in /home/dexter/.pm2/dump.pm2
[PM2] Generating system init script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -linux- Using the command:
      su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"

 System start/stop links for /etc/init.d/pm2-init.sh already exist.
[PM2] Done.

Nathanaël Lécaudé

unread,
Apr 5, 2016, 12:53:21 PM4/5/16
to Node-RED
I think you need to separate your args:

[
{
 "script"    : "/usr/local/bin/node-red -p 1880",

 
"args"     :"-p 1880",
 "name"      : "node-red default"
}
]

Cory Guynn

unread,
Apr 5, 2016, 1:41:35 PM4/5/16
to node...@googlegroups.com
Brilliant! I got it working.

~~ For the history books ~~

My process.json file:

[
{
  "script"    : "/usr/local/bin/node-red",
  "name"      : "node-red: 1880"
},
{
  "script"    : "/usr/local/bin/node-red",
  "args" : "/home/dexter/.node-red/iol.json -p 1881",
  "name"      : "node-red IoL Master: 1881"
}, 
{
  "script"    : "/usr/local/bin/node-red",
  "args" : "/home/dexter/.node-red/weather.json -p 1882",
  "name"      : "node-red weather: 1882"
},
{
  "script"    : "/usr/local/bin/node-red",
  "args" : "/home/dexter/.node-red/trainschedule.json -p 1883",
  "name"      : "node-red train schedule: 1883"
}
]



The output:

Inline image 1



THANK YOU!


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/2sboLs9ifjQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Julian Knight

unread,
Apr 5, 2016, 4:54:04 PM4/5/16
to Node-RED
If you are running on a modern version of Linux, maybe you should try using systemd instead of pm2. I found that pm2 was rather a resource hog and I'd often find everything slowing to a crawl or not responding to input. 

So until recently when I finally got rid of my old flows, I was running two instances of NR on different versions via systemd.

Cory Guynn

unread,
Apr 5, 2016, 5:15:16 PM4/5/16
to node...@googlegroups.com
Thanks for the suggestion. I do run Node-RED on Raspberry Pis or similar, so performance is a concern. 

This whole issue started with a flow consuming the max CPU. I had to split them all out to discover it was a Google Chart node pulling data from a MongoDB. After I figured out how to run them all independently, the Keymetrics site for PM2 identified the buggy flow.

Cheers



Nathanaël Lécaudé

unread,
Apr 5, 2016, 5:20:30 PM4/5/16
to node...@googlegroups.com
the Keymetrics site for PM2

Your paying for the service or is there a free version ?
--

Nathanaël Lécaudé

T (514) 843 8433

  

Cory Guynn

unread,
Apr 5, 2016, 7:31:14 PM4/5/16
to node...@googlegroups.com

Free version. One server.
I'm just experimenting on a dev box.

Krzysztof Karpiński

unread,
Jan 20, 2017, 9:39:29 AM1/20/17
to Node-RED
Hello,
That discuss is little old one, but have idea to split/ create more instances of node process, Im trying understand how to do that by using systemd on PI3 and Jessie.
Not succsesfull yet, Maybe somebody have experiences and can support how to begin that ?


Krzysztof

Dave C-J

unread,
Jan 20, 2017, 5:20:38 PM1/20/17
to node...@googlegroups.com
Nat's project template is one quick way to fire up instances of Node-RED on different ports from the command line, so you can have several in parallel if you like.

Krzysztof Karpiński

unread,
Jan 20, 2017, 6:13:04 PM1/20/17
to Node-RED

Thanks Dave I will also test that solution.


I the middle time - after a long evening I did test manually, maybe it can be helpful for somebody J

Just few things:

-          inside the folder /lib/system/system  copied the file nodered.service to nodeblue.service (so can continue with black, green and so on J)

-          edited tis file and change exec line to:

ExecStart=/usr/bin/env node-blue -v -u /home/pi/.node-red -s settings_blue.js -p 1881 flows_blue.json

            So have different config and flow file for second instance.

Next folder to work was /usr/bin

                Copied node-red-start, node-red-stop and node-red-log to *-blue ones, edited all them, changed messages (for fun) and important exec line to “sudo nodeblue start” also changed log to nodeblue

                At the end (the same folder) copied node-red to node-blue – I’m not sure if that is important and needed (that’s why have  ExecStart=/usr/bin/env node-blue in the nodeblue.service), here changed only one line “process.title = 'node-red';” to “process.title = 'node-blue';”

I wanted to see in the task list what I have:

root@raspberrypi:/usr/bin# ps axg | grep node-

16818 ?        SNsl  12:48 node-red

20105 ?        SNsl   0:13 node-blue

 

Possible that last change is not needed, so in the task list both processes will named “node-red”


Finally has run:  sudo systemctl enable nodeblue.service to enable autostart also second “blue” instance


Now can run node-red-start and node-blue-start (same stop and log) and have two node instances, the only what can’t change is the tab tittle in browser for both instances (in edit mode) are “Node-RED”, I think that's not really important J.

That exactly what I needed, As result have working main process and second “blue” for testing.

 

Krzysztof


Dave C-J

unread,
Jan 20, 2017, 6:44:38 PM1/20/17
to node...@googlegroups.com

If you do want to change the page name, you can using the editor theme in settings.js

Krzysztof Karpiński

unread,
Jan 20, 2017, 7:18:35 PM1/20/17
to Node-RED
Hi,

Thanks a lot Dave, I miss that,
It is exactly what I need.
Added:

editorTheme: {
        page: {
                title: "Node-BLUE",
        },
        header: {
                title: "Node-BLUE",
        },
    },

Jéan Roux

unread,
Jul 25, 2017, 12:30:14 PM7/25/17
to Node-RED
Hmmm, interesting approach. Have not tried it yet, but used docker to fire up multiple instances of NR, and then ea. with their own flows.json. doing this on RPi.

My requirement is as follows: I host a central DB for IoT measurements of water usage, (on one RPi - not with Docker), and the DB hosts several tables, where I can delineate measurements per client. Then, two RPi's with docker swarm mode, where I host NodeRed - that displays - an instance per client, their water usage stats.

Which is the better? Docker or this approach?

With docker - probably more portable to other environments, better delineation from a security perspective, but more resource hungry. 

What do you think?
Reply all
Reply to author
Forward
0 new messages