Node-RED won't run as service Debian 10

Hello all, I installed Node-RED using the script, as I try to set it up to run as service I got an error.

If you run node-red-start it will start it using the service but then the log will appear. Alternatively you can look back in /var/log/syslog to the time 12:39 and you will see it there.

Tip: node-red-log will just display that same tail of the log, and keep it updated as more comes in. However, since you're coming from a crashed state, node-red-start is the better option here. For future reference though :slight_smile:

sudo journalctl nodered will show you the last few lines of the log, adding the -f (follow) parameter will keep the log showing.

I recommend starting another terminal window to show the log in realtime - that way you can see exactly what is happening.

I've just gone through all this on my new build - though I use my own service file with my alternate installer.

That's what node-red-log does.

Ah, well I prefer to use native commands where possible - that way I also know how to show the logs for mosquitto, InfluxDB, Grafana, Telegraf, ... :grinning:

I guess I just like to know how things work!

Has anyone solved this error before?

Can you post here the service file /lib/systemd/system/nodered.service please. Copy/paste if possible rather than a screenshot, it is easier to read.
Did you run the install script as root by any chance? You should have run it logged in as the user you want to run node-red (which should not be root). If you did run it as root then I think you can just edit the service file and change the user, group and working directory. then run the sudo systemctl daemon-reload and try starting node-red again.

1 Like

For reference, here is my custom service file on my new build:

# A TEMPLATE systemd service file for running Node-RED
# CHANGE the settings before use.
#
# See https://github.com/TotallyInformation/alternate-node-red-installer
#     https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93
#     http://www.freedesktop.org/wiki/Software/systemd/
#
# This file must be linked into the /etc/systemd/system/multi-user.target.wants/ folder:
#    1) Rename this file to have a unique name (in case you want other instances running)
#    2) sudo /bin/systemctl enable /home/<user>/<folder>/system/<filename>.service
#    3) sudo systemctl daemon-reload
#
# After installing or changing this file:
#    1) sudo systemctl daemon-reload
#
# Start manually with `sudo /bin/systemctl start <filename>`
# View logs with `sudo journalctl -u <filename>` (e.g. `sudo journalctl -u node-red`)
# View service status with `sudo systemctl status <filename>`

[Unit]
Description=Knightnet Home Automation System - Node-RED nrmain
Documentation=https://github.com/TotallyInformation/alternate-node-red-installer http://nodered.org/docs
# Syslog and Networking must be active before starting
After=syslog.target network.target
# Mosquitto (MQTT) & InfluxDB may be wanted - ADJUST to need
Wants=mosquitto.service influxdb.service

[Service]
Type=simple

# -- Set any required environment variables - ADJUST these according to your installation --
Environment="NODE_RED_OPTIONS=--userDir /home/home/nrmain/data -v"
Environment="NODE_OPTIONS= "
#Environment="NODE_OPTIONS=--max-old-space-size=128"

# Or use an environment variables file, overrides the Environment settings
# makes it nice and easy to change without having to edit this file.
# Ignored if file does not exist. Entries in file overwrite those above.
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=
EnvironmentFile=-/home/home/nrmain/data/envfile.ini

# No path environment variable is yet available here so make sure absolute folders specified
ExecStart=/usr/bin/node $NODE_OPTIONS /home/home/nrmain/node_modules/node-red/red.js $NODE_RED_OPTIONS

WorkingDirectory=/home/home/nrmain

# 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=pi to User=node-red
# If changing the user/group, make sure that ALL FILES and FOLDERS have the correct
# ownership before continuing.
User=home
Group=home

Nice=10

SyslogIdentifier=NRMAIN
StandardOutput=syslog

# Make Node-RED restart if it fails
Restart=on-failure

#Restart=always
#RestartSec=100

# Node-RED needs a SIGINT to be notified to stop
KillSignal=SIGINT

# Prevent this service changing the OS or /etc
# @See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=
ProtectSystem=full

# PID file
PIDFile=/home/home/nrmain/nrmain.pid

[Install]
WantedBy=multi-user.target
1 Like

Is that path correct? I see it repeated over the other variables so I think it is, although an unusual name it would make sense, but it can lead to confusion :slight_smile:

Yes, correct. My own fault for choosing a default user name to match the purpose of the device! :scream_cat:

Only me directly using the device though so not really an issue.

This is what the standard Debian/pi install script installs (unless it has changed recently)

# systemd service file to start Node-RED

[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=username
Group=username
WorkingDirectory=/home/username

Nice=5
Environment="NODE_OPTIONS=--max_old_space_size=256"
# uncomment and edit next line if you need an http proxy
#Environment="HTTP_PROXY=my.httpproxy.server.address"
# uncomment the next line for a more verbose log output
#Environment="NODE_RED_OPTIONS=-v"
#ExecStart=/usr/bin/env node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
# Tag things in the log
SyslogIdentifier=Node-RED
#StandardOutput=syslog

[Install]
WantedBy=multi-user.target

Apart from running from a local install of Node-RED instead of a global one, the main difference in mine is the ability to override the environment vars and therefore the startup settings via a local file. It also requires syslog & networking along with Mosquitto and InfluxDB to be started first. Also I don't set max-old-space by default (because this was taken from the laptop build). Not sure if ProtectSystem=full actually does anything in this case but it seemed sensible.