Autostart on Debian

Stephan Tual edited this page Oct 29, 2017 · 40 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

NOTE: Make sure to remove any spaces before section that starts with "### BEGIN INIT INFO" and ends with "### END INIT INFO" failure to do so will result in errors like:

  • insserv: Script nzbdrone is broken: incomplete LSB comment
  • insserv: missing `Provides:' entry: please add
  • insserv: missing `Required-Start:' entry: please add even if empty
  • insserv: missing `Required-Stop:' entry: please add even if empty
  • insserv: missing `Default-Start:' entry: please add even if empty
  • insserv: missing `Default-Stop:' entry: please add even if empty
  • insserv: Default-Start undefined, assuming empty start runlevel(s) for script "nzbdrone"
  • insserv: Default-Stop undefined, assuming empty stop runlevel(s) for script "nzbdrone"
        
#!/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
or 
useradd --system nzbdrone (if following the security note in the next section)

Start Sonarr

sudo service nzbdrone start

Security Note

For security purposes, create a separate account to run this service and do not leave it as 'root'. 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. If You use the --system flag while creating a user, it will not have a home directory as listed below, so You may need to manually create one.

  /etc/passwd 'nzbdrone:x:1001:1001:NzbDrone Sonarr Media:/home/nzbdrone:/bin/false'
  /etc/shadow 'nzbdrone:*:17124:0:99999:7:::'

Utilizing the '/bin/false' 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, and the home directory, to only allow read, write and executable access to the directory owner and other users in the same group as this directory.

  sudo chmod 770 /opt/NzbDrone -R
  sudo chmod 770 /home/nzbdrone -R

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

  sudo chown nzbdrone:nzbdrone /opt/NzbDrone -R
  sudo chown nzbdrone:nzbdrone /home/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 -ld /home/nzbdrone/
  drwxrwx--- 4 nzbdrone nzbdrone 4096 Nov 23 17:36 /home/nzbdrone
  sudo 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. Replace "test123" with a valid user on the system.

  su - test123
  cd /opt/NzbDrone/
  -su cd: /opt/NzbDrone/: Permission denied
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.