Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
init.d script for Node-RED

This gist creates an init.d script to automatically run Node-RED at boot time.

To run it as a single step you can cut paste the following single line command

sudo wget -O /tmp/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod 755 /etc/init.d/nodered && sudo update-rc.d nodered defaults

or click right-click "save as" this link - But please read the script first and satisfy yourself it's safe to execute in your environment...

#! /bin/sh
# Starts and stops Node-RED
# /etc/init.d/nodered
# Provides: node-red
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Node-RED initialisation
# Can be downloaded and installed in one go by using this command
# sudo wget -O /tmp/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod 755 /etc/init.d/nodered && sudo update-rc.d nodered defaults
# This runs as the user called pi - please change as you require
# The log is written to here - please make sure your user has write permissions.
#Load up node red when called
case "$1" in
if pgrep ^node-red$ > /dev/null
echo "Node-RED already running."
echo "Starting Node-Red.."
touch $LOG
chown $USER:$USER $LOG
echo "" >> $LOG
echo "Node-RED service start: "$(date) >> $LOG
# su -l $USER -c "cd ~/.node-red && screen -dmS red node-red-pi --max-old-space-size=128"
# or
su -l $USER -c "node-red-pi --max-old-space-size=128 -u ~/.node-red >> $LOG &"
echo "Logging to "$LOG
echo "Stopping Node-Red.."
# su -l $USER -c "screen -S red -X quit"
# or
pkill -SIGINT ^node-red$
sleep 2
echo "" >> $LOG
echo "Node-RED service stop: "$(date) >> $LOG
echo "Restarting Node-Red.."
$0 stop
sleep 2
$0 start
echo "Restarted."
echo "Usage: $0 {start|stop|restart}"
exit 1
ukmoose commented Sep 3, 2014

looks like line 23 has been truncated?
su -l pi -c "cd node-red; screen -dmS red node --max-old-space-size=128$

should it be
su -l pi -c "cd node-red; screen -dmS red node --max-old-space-size=128 red.js"

Hi, I'm trying to run this on a machine with nvm installed. I've set nvm to use 0.12.8 and running node-red from the command line works fine and picks up that version. But using this service, node-red picks up v5.0.0 of node instead. Any idea how I might set version for this script as well?

gbrian commented Sep 23, 2016


In my case I moved from grep to pidof as I was having strange behavior.

gibfahn commented Nov 4, 2016

@sinewave440hz You'd need to run the nvm script that runs when you use the command line. When you start a shell a file will be run (possibly ~/.bashrc or ~/.profile), and at the end of it nvm appends a line which says something like:

if [ -s "$NVM_DIR/" ]; then
    . "$NVM_DIR/"

You need to add those lines to this script, probably after echo "Starting Node-Red.."

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