diff --git a/Autostart-on-Linux.md b/Autostart-on-Linux.md index aa931ee..f88c548 100644 --- a/Autostart-on-Linux.md +++ b/Autostart-on-Linux.md @@ -74,121 +74,191 @@ exec mono --debug $DIR/Radarr.exe sudo start radarr -## LSB init-script (Debian/Ubuntu) +## Init.d Script (Debian/Ubuntu) +# Autostart on Debian using init.d script -### Init File -The init.d file should be saved as `/etc/init.d/radarr` +The instructions for Ubuntu/Debian did not seem to work for Debian very well, and the pid got lost whenever Radarr was restarted via the web-ui, so I've included a working script here init.d script here. You'll need to have already created an radarr user. -```bash +**Create and edit the radarr init.d script** + + sudo nano /etc/init.d/radarr - #!/bin/bash - - ### BEGIN INIT INFO - # Provides: radarr - # Required-Start: $local_fs $network $remote_fs - # Required-Stop: $local_fs $network $remote_fs - # Default-Start: 2 3 4 5 - # Default-Stop: 0 1 6 - # Short-Description: radarr - # Description: Radarr - ### END INIT INFO - - . /lib/lsb/init-functions - - #set -e - - NAME=radarr - DESC="Radarr" - MONO=$(which mono) - - DAEMON=/opt/radarr/Radarr.exe - DAEMONOPTS="" - - PIDDIR=/var/run/${NAME} - PIDFILE=${PIDDIR}/${NAME}.pid - - RUNASUSER=user - RUNASGROUP=group - RUNAS=$RUNASUSER:$RUNASGROUP +**Paste the following, changing applicable variables** +````bash + +#! /bin/sh +### BEGIN INIT INFO +# Provides: Radarr +# Required-Start: $local_fs $network $remote_fs +# Required-Stop: $local_fs $network $remote_fs +# Should-Start: $NetworkManager +# Should-Stop: $NetworkManager +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts instance of Radarr +# Description: starts instance of Radarr using start-stop-daemon +### END INIT INFO - DATADIR=/home/$RUNASUSER/ - - if ! [ -r ${DAEMON} ]; then echo "Can't read: ${DAEMON}" 2>&1; exit 1; fi - if ! [ -x ${MONO} ]; then echo "Not executable: ${MONO}" 2>&1; exit 1; fi - if ! [ -d ${DATADIR} ]; then echo "No such directory: ${DATADIR}" 2>&1; exit 1; fi - - if [ ! -d ${PIDDIR} ]; then - mkdir -p ${PIDDIR}; chown ${RUNASUSER}:root ${PIDDIR}; chmod 0750 ${PIDDIR}; +############### EDIT ME ################## +# path to app +APP_PATH=/opt/Radarr + +# user +RUN_AS=root + +# path to mono bin +DAEMON=$(which mono) + +# Path to store PID file +PID_FILE=/var/run/radarr/radarr.pid +PID_PATH=$(dirname $PID_FILE) + +# script name +NAME=radarr + +# app name +DESC=Radarr + +# startup args +EXENAME="Radarr.exe" +DAEMON_OPTS=" "$EXENAME + +############### END EDIT ME ################## + +RADARR_PID=`ps auxf | grep Radarr.exe | grep -v grep | awk '{print $2}'` + +test -x $DAEMON || exit 0 + +set -e + +#Look for PID and create if doesn't exist +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 - - do_start() { - RETVAL=1 - if [ -e ${PIDFILE} ]; then - if ! kill -0 $(cat ${PIDFILE}) &> /dev/null; then - rm -f $PIDFILE - fi - fi - - log_daemon_msg "Starting ${DESC}" "${NAME}" - if pgrep -f "^${MONO} ${DAEMON}" > /dev/null 2>&1; then - log_progress_msg "(already running?)" +fi + +echo $RADARR_PID > $PID_FILE + +case "$1" in + start) + if [ -z "${RADARR_PID}" ]; then + echo "Starting $DESC" + rm -rf $PID_PATH || return 1 + install -d --mode=0755 -o $RUN_AS $PID_PATH || return 1 + start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS + else + echo "Radarr already running." + fi + ;; + stop) + echo "Stopping $DESC" + echo $RADARR_PID > $PID_FILE + start-stop-daemon --stop --pidfile $PID_FILE --retry 15 + ;; + + restart|force-reload) + echo "Restarting $DESC" + start-stop-daemon --stop --pidfile $PID_FILE --retry 15 + start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS + ;; + status) + # Use LSB function library if it exists + if [ -f /lib/lsb/init-functions ]; then + . /lib/lsb/init-functions + if [ -e $PID_FILE ]; then + status_of_proc -p $PID_FILE "$DAEMON" "$NAME" && exit 0 || exit $? else - start-stop-daemon -q -d ${DATADIR} -c $RUNAS --start --background --make-pidfile --pidfile $PIDFILE --exec $MONO -- $DAEMON $DAEMON_OPTS - RETVAL=$? + log_daemon_msg "$NAME is not running" + exit 3 fi - log_end_msg $RETVAL - } - do_stop() { - RETVAL=1 - log_daemon_msg "Stopping ${DESC}" "${NAME}" - if ! pgrep -f "^${MONO} ${DAEMON}" > /dev/null 2>&1; then - log_progress_msg "(not running?)" + + else + # Use basic functions + if [ -e $PID_FILE ]; then + PID=`cat $PID_FILE` + if kill -0 $PID > /dev/null 2>&1; then + echo " * $NAME is running" + exit 0 + fi else - start-stop-daemon -q --stop --pidfile $PIDFILE --retry 15 - RETVAL=$? + echo " * $NAME is not running" + exit 3 fi - log_end_msg $RETVAL - - } - - case "$1" in - start) - do_start - ;; - stop) - do_stop - ;; - - status) - status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $? - ;; - - restart|force-reload) - do_stop; - do_start; - ;; - *) - N=/etc/init.d/$NAME - echo "Usage: $0 {start|stop|status|restart|force-reload}" >&2 - exit 1 - ;; - esac - - exit 0 -``` + fi + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|restart|force-reload|status}" >&2 + exit 1 + ;; +esac -### Make init.d script executable -`sudo chmod +x /etc/init.d/radarr` +exit 0 -### Run insserv -`sudo insserv -v radarr` +```` -Note: May need to run `sudo ln -s /usr/lib/insserv/insserv /sbin/insserv` first if insserv is not found. +**Make it executable** -## Start init.d script -`sudo /etc/init.d/radarr start` + sudo chmod +x /etc/init.d/radarr -Note: The script by default runs as root. I suggest adding an radarr user (`useradd -m -d /var/lib/radarr --gid nogroup radarr`) and adjusting the script so that RUNASUSER=radarr & RUNASGROUP=nogroup. +**Update rc.d** + + sudo update-rc.d radarr defaults + +**Create radarr user** + + useradd radarr + +**Start Radarr** + + sudo service radarr start + +**Security Note** + +For security purposes, you can prevent this account from being logged into by editing the '/etc/passwd' entry for user radarr and changing it to '/bin/false' or /usr/sbin/nologin'. I've also removed the password from '/etc/shadow'. From researching, these both appear to be the manual ways to perform the '--disable-login' and '--disable-password' while creating a new user. Alternatively, you can accomplish this same task by creating a user with the '--system' option. + + /etc/passwd 'radarr:x:1001:1001:Radarr Movie Media:/opt/Radarr:/bin/false' + /etc/shadow 'radarr:*:17124:0:99999:7:::' + +Utilizing the '/usr/sbin/nologin' option will output what's seen below, when attempting to change to that user from root. + + su - radarr + This account is currently not available. + +Now, recursively edit the /opt/Radarr directory to only allow read, write and executable access to the directory owner and other users in the same group as this directory. + + chmod 770 /opt/Radarr -R + +Add the directory to the group 'radarr' and modify the owner to 'radarr' + + chown radarr:radarr /opt/Radarr -R + +If all is well, you should see the following outputs. + + ls -ld /opt/Radarr/ + drwxrwx--- 4 radarr radarr 4096 Nov 23 17:31 /opt/Radarr/ + + ls -la /opt/Radarr/Radarr.exe + -rwxrwx--- 1 radarr radarr 23552 Nov 6 13:05 /opt/Radarr/Radarr.exe + +Test from a different user account to confirm these user and group permissions + + su - test123 + cd /opt/Radarr/ + -su cd: /opt/Radarr/: Permission denied ## FreeBSD/FreeNAS https://raw.github.com/tofagerl/freedrone/master/nzbdrone