Autostart on Debian

legoman666 edited this page Dec 22, 2016 · 26 revisions

Autostart on Debian using init.d script

The instructions for Ubuntu/Debian did not seem to work for Debian very well, and the pid got lost whenever Sonarr 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 nzbdrone user.

Create and edit the nzbdrone init.d script

sudo vim /etc/init.d/nzbdrone

Paste the following, changing applicable variables

#!/bin/sh -
### BEGIN INIT INFO
# Provides:          NzbDrone
# 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 Sonarr
# Description:       starts instance of Sonarr using start-stop-daemon
### END INIT INFO

############### EDIT ME ##################
# path to app
APP_PATH=/opt/NzbDrone

# user
RUN_AS=nzbdrone

# path to mono bin
DAEMON=/usr/bin/mono

# options for mono
DAEMON_OPTS=""

# Path to store PID file
PID_PATH=/var/run/nzbdrone
############### END EDIT ME ##################
mkdir -p ${PID_PATH}
PID_FILE=${PID_PATH}/nzbdrone.pid
EXENAME=`basename ${APP_PATH}/NzbDrone.exe`
DESC=`basename ${APP_PATH}/NzbDrone.exe .exe`
NZBDRONE_PID=`ps auxf | grep $EXENAME | grep -v grep | awk '{print $2}'`

test -x $DAEMON || { echo "$DAEMON must be executable."; exit 1; }

set -e

echo $NZBDRONE_PID > $PID_FILE

case "$1" in
start)
    if [ -z "${NZBDRONE_PID}" ]; then
        echo "Starting $DESC"
        rm ${PID_FILE} || 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 $EXENAME
    else
        echo "${DESC} already running."
    fi
    ;;
stop)
    echo "Stopping $DESC"
    echo $NZBDRONE_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 $EXENAME
    ;;
*)
     echo "Usage: `basename $0` {start|stop|restart|force-reload}" >&2    
     exit 1
    ;;   

esac

exit 0

Make it executable

sudo chmod +x /etc/init.d/nzbdrone

Update rc.d

sudo update-rc.d nzbdrone defaults

Create nzbdrone user

useradd nzbdrone

Start Sonarr

sudo service nzbdrone start

Security Note

For security purposes, you can prevent this account from being logged into by editing the '/etc/passwd' entry for user nzbdrone 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 'nzbdrone:x:1001:1001:NzbDrone Sonarr Media:/opt/NzbDrone:/bin/false'
  /etc/shadow 'nzbdrone:*: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 - nzbdrone
  This account is currently not available.

Now, recursively edit the /opt/NzbDrone 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/NzbDrone -R

Add the directory to the group 'nzbdrone' and modify the owner to 'nzbdrone'

  chown nzbdrone:nzbdrone /opt/NzbDrone -R

If all is well, you should see the following outputs.

  ls -ld /opt/NzbDrone/
  drwxrwx--- 4 nzbdrone nzbdrone 4096 Nov 23 17:31 /opt/NzbDrone/  

  ls -la /opt/NzbDrone/NzbDrone.exe
  -rwxrwx--- 1 nzbdrone nzbdrone 23552 Nov  6 13:05 /opt/NzbDrone/NzbDrone.exe

Test from a different user account to confirm these user and group permissions

  su - test123
  cd /opt/NzbDrone/
  -su cd: /opt/NzbDrone/: Permission denied