Skip to content

Instantly share code, notes, and snippets.

@hdml
Last active March 17, 2016 01:23
Show Gist options
  • Save hdml/e7f1c17e52b3dc4b8993 to your computer and use it in GitHub Desktop.
Save hdml/e7f1c17e52b3dc4b8993 to your computer and use it in GitHub Desktop.
Pi-Signage node as a system service in CentOS 7

##Pi-Signage node as a system service ###Installing a node instance as a system service in CentOS 7

You need a user with sudo privileges, but limited to running node. This example will use the user mediaservice

$ visudo

Change the line Defaults: requiretty

To

Defaults:mediaservice    !requiretty

At the end of the sudoers file, add the next line

mediaservice ALL = NOPASSWD: /usr/bin/node

This will ensure mediaservice can only run as root the node process.

##Install the Pi-Signage System Service Note: always inspect what you download, be sure to read the downloaded script using less /etc/init.d/pi-signage

$ cd /etc/init.d
$ sudo wget https://gist.githubusercontent.com/hdml/e7f1c17e52b3dc4b8993/raw/1b4365a72a077e8541c13b256d97c973cdfd3e94/pi-signage
$ sudo chmod +x pi-signage

Register the new service

$ sudo chkconfig --add pi-signage
$ systemctl daemon-reload

Check that the service registered successfully

$ chkconfig --list

##Start the service

$ sudo service pi-signage start

Note: if the service fails to start, check systemctl status pi-signage.service. If there's an error that says the server is locked, remove the file in /var/lock/subsys/node-server.

#!/bin/sh
#
# chkconfig: 35 99 99
# description: Pi-Signage Node Server
#
. /etc/rc.d/init.d/functions
USER="mediaservice"
DAEMON="/usr/bin/node"
ROOT_DIR="/home/mediaservice/pisignage-server"
SERVER="$ROOT_DIR/server.js"
LOG_FILE="$ROOT_DIR/server.js.log"
LOCK_FILE="/var/lock/subsys/node-server"
do_start()
{
if [ ! -f "$LOCK_FILE" ] ; then
runuser -l "$USER" -c "cd $ROOT_DIR; sudo $DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
else
echo "$SERVER is locked."
RETVAL=1
fi
}
do_stop()
{
echo -n $"Stopping $SERVER: "
pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment