diff --git a/init/ubuntu b/init/ubuntu index dac2076..511d9ee 100644 --- a/init/ubuntu +++ b/init/ubuntu @@ -26,48 +26,89 @@ NAME=couchpotato # App name DESC=CouchPotato -# Path to app root -CP_APP_PATH=${APP_PATH-/usr/local/sbin/CouchPotatoServer/} +## Don't edit this file +## Edit user configuation in /etc/default/couchpotato to change +## +## CP_USER= #$RUN_AS, username to run couchpotato under, the default is couchpotato +## CP_HOME= #$APP_PATH, the location of couchpotato.py, the default is /opt/couchpotato +## CP_DATA= #$DATA_DIR, the location of couchpotato.db, cache, logs, the default is /var/couchpotato +## CP_PIDFILE= #$PID_FILE, the location of couchpotato.pid, the default is /var/run/couchpotato.pid +## PYTHON_BIN= #$DAEMON, the location of the python binary, the default is /usr/bin/python +## CP_OPTS= #$EXTRA_DAEMON_OPTS, extra cli option for couchpotato, i.e. " --config_file=/home/couchpotato/couchpotato.ini" +## SSD_OPTS= #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users" +## +## EXAMPLE if want to run as different user +## add CP_USER=username to /etc/default/couchpotato +## otherwise default couchpotato is used + +# Run CP as username +RUN_AS=${CP_USER-couchpotato} + +# Path to app +# CP_HOME=path_to_app_CouchPotato.py +APP_PATH=${CP_HOME-/opt/couchpotato/} + +# Data directory where couchpotato.db, cache and logs are stored +DATA_DIR=${CP_DATA-/var/couchpotato} -# User to run CP as -CP_RUN_AS=${RUN_AS-root} +# Path to store PID file +PID_FILE=${CP_PID_FILE-/var/run/couchpotato.pid} -# Path to python bin -CP_DAEMON=${DAEMON_PATH-/usr/bin/python} +# path to python bin +DAEMON=${PYTHON_BIN-/usr/bin/python} -# Path to store PID file -CP_PID_FILE=${PID_FILE-/var/run/couchpotato.pid} +# Extra daemon option like: CP_OPTS=" --config=/home/couchpotato/couchpotato.ini" +EXTRA_DAEMON_OPTS=${CP_OPTS-} + +# Extra start-stop-daemon option like START_OPTS=" --group=users" +EXTRA_SSD_OPTS=${SSD_OPTS-} + + +PID_PATH=`dirname $PID_FILE` +DAEMON_OPTS=" CouchPotato.py --quiet --daemon --pid_file=${PID_FILE} --data_dir=${DATA_DIR} ${EXTRA_DAEMON_OPTS}" -# Other startup args -CP_DAEMON_OPTS=" CouchPotato.py --daemon --pid_file=${CP_PID_FILE}" -test -x $CP_DAEMON || exit 0 +test -x $DAEMON || exit 0 set -e -. /lib/lsb/init-functions +# Create PID directory if not exist and ensure the CouchPotato user can write to it +if [ ! -d $PID_PATH ]; then + mkdir -p $PID_PATH + chown $RUN_AS $PID_PATH +fi + +if [ ! -d $DATA_DIR ]; then + mkdir -p $DATA_DIR + chown $RUN_AS $DATA_DIR +fi + +if [ -e $PID_FILE ]; then + PID=`cat $PID_FILE` + if ! kill -0 $PID > /dev/null 2>&1; then + echo "Removing stale $PID_FILE" + rm $PID_FILE + fi +fi case "$1" in start) echo "Starting $DESC" - rm -rf $CP_PID_FILE || return 1 - touch $CP_PID_FILE - chown $CP_RUN_AS $CP_PID_FILE - start-stop-daemon -d $CP_APP_PATH -c $CP_RUN_AS --start --background --pidfile $CP_PID_FILE --exec $CP_DAEMON -- $CP_DAEMON_OPTS + start-stop-daemon -d $APP_PATH -c $RUN_AS $EXTRA_SSD_OPTS --start --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS ;; stop) echo "Stopping $DESC" - start-stop-daemon --stop --pidfile $CP_PID_FILE --retry 15 + start-stop-daemon --stop --pidfile $PID_FILE --retry 15 ;; restart|force-reload) echo "Restarting $DESC" - start-stop-daemon --stop --pidfile $CP_PID_FILE --retry 15 - start-stop-daemon -d $CP_APP_PATH -c $CP_RUN_AS --start --background --pidfile $CP_PID_FILE --exec $CP_DAEMON -- $CP_DAEMON_OPTS + start-stop-daemon --stop --pidfile $PID_FILE --retry 15 + start-stop-daemon -d $APP_PATH -c $RUN_AS $EXTRA_SSD_OPTS --start --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS ;; status) - status_of_proc -p $CP_PID_FILE "$CP_DAEMON" "$NAME" + status_of_proc -p $PID_FILE "$DAEMON" "$NAME" ;; *) N=/etc/init.d/$NAME diff --git a/init/ubuntu.default b/init/ubuntu.default index 0d1e712..00c523e 100644 --- a/init/ubuntu.default +++ b/init/ubuntu.default @@ -1,5 +1,5 @@ -# COPY THIS FILE TO /etc/default/couchpotato -# OPTIONS: APP_PATH, RUN_AS, DAEMON_PATH, CP_PID_FILE +# COPY THIS FILE TO /etc/default/couchpotato +# OPTIONS: CP_HOME, CP_USER, CP_DATA, CP_PIDFILE, PYTHON_BIN, CP_OPTS, SSD_OPTS -APP_PATH= -RUN_AS=root \ No newline at end of file +CP_HOME= +CP_USER=root \ No newline at end of file