Skip to content

Instantly share code, notes, and snippets.

@Belphemur
Last active November 22, 2022 17:20
Show Gist options
  • Save Belphemur/3f6d3bf211b0e8a18d93 to your computer and use it in GitHub Desktop.
Save Belphemur/3f6d3bf211b0e8a18d93 to your computer and use it in GitHub Desktop.
# This script work on any system using systemd as the init process.
# It works on Debian/Raspbian Jessie.
# If you have Debian/Rapbian Wheezy and want to use this script with systemd
# follow the information here : https://wiki.debian.org/systemd
# To easily download, install and set at startup:
# wget -O /tmp/download https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/systemd/system/ && sudo systemctl --reload-daemon && sudo systemctl enable Node-RED
# To consult the log : journalctl -u Node-RED
[Unit]
Description=Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.
After=syslog.target network.target
Documentation=http://nodered.org/
[Service]
Environment="NODE_OPTIONS=--max-old-space-size=128"
Environment="NODE_RED_OPTIONS=-v"
#Full Path to Node.js
ExecStart=/usr/local/bin/node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
WorkingDirectory=/home/pi/node-red/
# User/Group that launches node-RED (it's advised to create a new user for Node-RED)
# You can do : sudo useradd node-red
# then change the User=root by User=node-red
User=root
Group=root
Nice=10
SyslogIdentifier=Node-RED
StandardOutput=syslog
# Make Node-RED restart if it fails
Restart=on-failure
# Node-RED need a SIGINT to be notified to stop
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
@krisklosterman
Copy link

krisklosterman commented Oct 28, 2016

This is what I did on Debian 8

I installed Node Red with NPM as global
I Created a user named nodered

I then ran these commands as root or with sudo

wget -O /etc/systemd/system/Node-Red.service https://gist.githubusercontent.com/Belphemur/3f6d3bf211b0e8a18d93/raw/3a33f9aaf1d07f5ad0fc2fe7aec2b70410a87111/Node-RED.service

vim /etc/systemd/system/Node-Red.service
You then need to modify it and change users and groups and change the following lines:

ExecStart to ExecStart=/usr/bin/node-red

WorkingDirectory to WorkingDirectory=/home/nodered/.node-red/ (This is because I used a user I created called nodered)

you can delete the two Environment lines too

then run

systemctl daemon-reload

systemctl enable Node-Red.service

systemctl start Node-Red.service

@mkeyno
Copy link

mkeyno commented Mar 27, 2017

@krisklosterman I made your suggestion and when I run sudo systemctl status nodered.service it shows following respond

nodered.service - The NodeRed
   Loaded: loaded (/lib/systemd/system/nodered.service; enabled)
   Active: active (running) since Mon 2017-03-27 22:05:51 UTC; 1s ago
 Main PID: 2501 (node)
   CGroup: /system.slice/nodered.service
           └─2501 node /usr/bin/node-red

Mar 27 22:05:51 KeynoGC systemd[1]: Started The NodeRed.

but http://localhost:1880; not respond , what did I miss

@mkeyno
Copy link

mkeyno commented Mar 28, 2017

now its work

[Unit]
Description=The NodeRed
After=syslog.target network.target

[Service]
Type=simple
User=root
Group=root
Nice=5
KillSignal=SIGINT
ExecStart=/usr/bin/node-red
 Restart=on-failure
 StandardOutput=syslog
WorkingDirectory=/home/pi/.node-red
User=root
Group=root
[Install]
WantedBy=multi-user.tar

``

@papaiatis
Copy link

WorkingDirectory does not really have any effect, but the following works:
ExecStart=/usr/bin/node-red -u /home/pi/.node-red where the -u switch specifies the user working directory for Node-RED itself.

@rvt
Copy link

rvt commented Jun 15, 2019

To Add to this script, running node-red as root is a bad idea, don´t do that

@papaiatis Assuming you User and Group to a non root user (for example the pi user) there is no reason to set the -u flag

@jayjupdhig
Copy link

When i only enter that...

wget -O /tmp/download https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/systemd/system/

..i get that error at the end:

gzip: stdin has more than one entry--rest ignored
tar: Child returned status 2
tar: Error is not recoverable: exiting now

Why that?

@jayjupdhig
Copy link

Problem is HERE:

root@debian-influx:~/.node-red# tar -zxf /tmp/download --strip-components 1 -C /etc/systemd/system/gzip: stdin has more than one entry--rest ignored
tar: Child returned status 2
tar: Error is not recoverable: exiting now

@generalinterest
Copy link

The /tmp/download is a zip file, not a gzip file. On my Ubuntu system

sudo apt install unzip

wget -O /tmp/download https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93/download

unzip /tmp/download

Now edit the 3f6d3bf211b0e8a18d93-master/Node-RED.service file and make any changes you like as per the prior posts here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment