-
-
Save DaveThw/da394c8d04fa2bba55ac to your computer and use it in GitHub Desktop.
init.d script for Node-RED on a Raspberry Pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: node-red | |
# Required-Start: $local_fs $remote_fs $network | |
# Required-Stop: $local_fs $remote_fs $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start or stop the node-red server | |
### END INIT INFO | |
# Can be downloaded and installed in one go by using this command | |
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/da394c8d04fa2bba55ac/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/node-red && sudo update-rc.d node-red defaults | |
# Or download and just move to /etc/init.d using this command | |
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/da394c8d04fa2bba55ac/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/node-red | |
# and then start/stop/restart the service with these commands | |
# sudo service node-red stop | |
# sudo service node-red start | |
# sudo service node-red restart | |
# And then, if/when you want Node-RED to autostart on bootup, use this | |
# sudo update-rc.d node-red defaults | |
# Conversely, if you want to stop Node-RED autostarting on bootup, use this | |
# sudo update-rc.d -f node-red remove | |
# User that launches node-RED (it's advised to create a new user for Node-RED) | |
# You can do: | |
# sudo useradd node-red | |
# then change line below from USER=pi to USER=node-red | |
USER=pi | |
# The location of Node-RED | |
# This is where Node-RED was installed by default before Node-RED v.0.10.4 | |
#SCRIPT_DIR='/home/pi/node-red/' | |
# From v.0.10.4 onwards, Node-RED is now installed here | |
SCRIPT_DIR='/usr/lib/node_modules/node-red/' | |
# Maximum amount of memory used by garbage before it is garbage collected (needs to be lower than the default 512 on a RaspberryPi) | |
# Use free -h to see how much free memory you have (before starting Node-RED!) | |
#MAX_OLD_SPACE_SIZE=64 | |
MAX_OLD_SPACE_SIZE=128 | |
#MAX_OLD_SPACE_SIZE=256 | |
# Verbose logging? Comment this next line out (ie. put a '#' at the beginning) for less verbose logging | |
# (in particular, with this set Node-RED will log details on any nodes that fail to register during startup) | |
VERBOSE='-v' | |
# DON'T CHANGE anything below this line unless you know what you're doing! | |
NAME=node-red | |
# Previously (Raspberry Pi 1?), Node was installed here: | |
# /usr/local/bin/node | |
# Node is now seems to be installed here (Raspberry Pi 2, with Node-RED v.0.10.6, as of May 2015...): | |
# /usr/bin/node | |
DAEMON=$(which node) | |
if [ ! -x "$DAEMON" ]; then | |
echo "Error: Unable to find node - is it installed?" | |
exit 1 | |
fi | |
OPTIONS="--max-old-space-size=${MAX_OLD_SPACE_SIZE} red.js ${VERBOSE}" | |
LOG="/var/log/${NAME}.log" | |
PIDFILE="/var/run/${NAME}.pid" | |
. /lib/lsb/init-functions | |
start_daemon () { | |
# If the log file doesn't yet exist, attempt to create it | |
! [ -e "$LOG" ] && sudo touch "$LOG" | |
# Check if USER is the owner of the log file, and chown if not | |
[ -e "$LOG" ] && [ -z "$(find "$LOG" -user $USER)" ] && sudo chown $USER "$LOG" | |
start-stop-daemon --start --background \ | |
--chuid $USER \ | |
--chdir $SCRIPT_DIR \ | |
--name $NAME \ | |
--make-pidfile --pidfile $PIDFILE \ | |
--startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1" | |
# check if the daemon actually started, and return an appropriate result with log_end_msg | |
status="0" | |
pidofproc $DAEMON >/dev/null || status="$?" | |
log_end_msg $status | |
} | |
stop_daemon () { | |
status="0" | |
start-stop-daemon --stop --signal INT --quiet \ | |
--chuid $USER \ | |
--chdir $SCRIPT_DIR \ | |
--exec $DAEMON --pidfile $PIDFILE --retry 30 \ | |
--oknodo || status="$?" | |
log_end_msg $status | |
} | |
case "$1" in | |
start) | |
log_daemon_msg "Starting daemon" "$NAME" | |
start_daemon | |
;; | |
stop) | |
log_daemon_msg "Stopping daemon" "$NAME" | |
stop_daemon | |
;; | |
restart) | |
log_daemon_msg "Stopping daemon" "$NAME" | |
stop_daemon | |
sleep 5 | |
log_daemon_msg "Re-Starting daemon" "$NAME" | |
start_daemon | |
;; | |
status) | |
status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" | |
exit $? | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|status}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment