System V init script(start-stop-daemon)

I’ve been looking for efficient ways to start at boot my NodeJS dependent applications, with inspiration from https://gist.github.com/alobato/1968852, I modified it to my own needs.

Link might interest you as well:
http://big-elephants.com/2013-01/writing-your-own-init-scripts/
https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

Copy template to /etc/init.d and rename it to something meaningful. Then edit the script and enter that name after Provides:(between ### BEGIN INIT INFO and ### END INIT INFO).

#!/bin/bash
# Inspired by https://gist.github.com/alobato/1968852
# Needs Provides, Descriptions

### BEGIN INIT INFO
# Provides:
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description:
# Description:
### END INIT INFO

set -e
NAME=""
PIDFILE="/run/$NAME/$NAME.pid"
# Application one wants to upstart
DAEMON=""
DAEMON_OPTS=""
# Run as user
RUN_USER=""
RUN_GROUP=""
function daemon_run {
	mkdir -p /run/$NAME
	chown $RUN_USER:$RUN_GROUP /run/$NAME
	start-stop-daemon --start --background --quiet --chuid $RUN_USER:$RUN_GROUP --chdir /run/$NAME --pidfile $PIDFILE --make-pidfile --exec $DAEMON $DAEMON_OPTS
}
exec > /var/log/$NAME.log 2>&1

case "$1" in
start)
	echo -n "Starting $NAME ... "
	daemon_run
	echo "done."
;;

silent)
	echo -n "Starting $NAME in silent mode ... "
	daemon_run
	echo "done."
;;

stop)
	echo -n "Stopping $NAME ... "
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --remove-pidfile
	echo "done."
;;
restart|force-reload)
	echo -n "Restarting $NAME ... "
	start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE --remove-pidfile
	alexa_run
	echo "done."
;;
*)
	echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0

When done,

sudo systemctl enable 'name_of_filename'
reboot

Mpho