Proposed change to RPi init.d service file: non-root user

All the topics related to QLC+ on the Raspberry Pi
Post Reply
User avatar
rawbengal
Posts: 18
Joined: Fri Jul 10, 2015 5:23 am
Location: Los Angeles, CA, USA
Real Name: Rob Engle

I generally prefer to have programs run as non-root users when possible. I modified the /etc/init.d/qlcplus file to allow one to specify the user ID and for the home directory to properly map to the chosen user. Attached is the new version. I am using the 'pi' user on my machine but this version of the file leaves the user name as 'root' to remain backward compatible with existing installations.

cheers,
Rob

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides:		qlcplus
# Required-Start:	$network $remote_fs $syslog $time
# Required-Stop:	$network $remote_fs $syslog $time
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Q Light Controller Plus
# Description:		Q Light Controller Plus
### END INIT INFO

NAME=qlcplus
DESC="Q Light Controller Plus"
BINARY=/usr/bin/qlcplus

test -x $BINARY || exit 0

. /lib/lsb/init-functions

QLCPLUS_OPTS="--web --operate --overscan"
export USER=root     # change to 'pi' or another unprivileged user

export HOME=$(getent passwd "$USER" | cut -d: -f6)
cd $HOME
autostart="$HOME/.qlcplus/autostart.qxw"
if [ -e "$autostart" ]; then
        QLCPLUS_OPTS="$QLCPLUS_OPTS --open $autostart"
fi

# if NTP hasn't done its job already, set the date to modern age...
CURRDATE=`date +%Y`
if [ "$CURRDATE" -lt "2016" ]; then
date +%Y%m%d -s "20160415"
fi

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --start --background --oknodo --quiet -m --pidfile /var/run/qlcplus.pid --chuid "$USER" --exec $BINARY -- $QLCPLUS_OPTS >/dev/null
        log_end_msg "$?"
        ;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	start-stop-daemon --stop --oknodo --quiet --exec $BINARY --pidfile /var/run/qlcplus.pid
	log_end_msg "$?"
	;;
  reload)
  	log_daemon_msg "Reloading $DESC" "$NAME"
	start-stop-daemon --stop --signal HUP --exec $BINARY --pidfile /var/run/qlcplus.pid
	log_end_msg "$?"
	;;
  restart|force-reload)
  	$0 stop
	sleep 8
	$0 start
	;;
  status)
	status_of_proc $BINARY "$NAME"
	exit $?
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
	exit 1
	;;
esac

exit 0
Post Reply