29 Autostart on Linux
Logvin edited this page 4 years ago

Systemd on Linux - Radarr V0.2

Most modern Linux distributions have switched to systemd, which involves a simple service file which gets enabled and started.

Service File

Be sure to change the User, Group and path for both mono and Radarr.exe to match your installation. The file should be named radarr.service and the best place for it is /etc/systemd/system/. Alternative locations like /usr/lib/systemd/system/ and /lib/systemd/system/ may depend on the distribution used. This example unit assumes that Radarr's executable is placed in /opt/Radarr.

[Unit]
Description=Radarr Daemon
After=syslog.target network.target

[Service]
# Change the user and group variables here.
User=radarr
Group=radarr

Type=simple

# Change the path to Radarr or mono here if it is in a different location for you.
ExecStart=/usr/bin/mono --debug /opt/Radarr/Radarr.exe -nobrowser
TimeoutStopSec=20
KillMode=process
Restart=on-failure

# These lines optionally isolate (sandbox) Radarr from the rest of the system.
# Make sure to add any paths it might use to the list below (space-separated).
#ReadWritePaths=/opt/Radarr /path/to/movies/folder
#ProtectSystem=strict
#PrivateDevices=true
#ProtectHome=true

[Install]
WantedBy=multi-user.target

The option -data=<AppData Location> can also be used to change radarr's AppData location. See https://github.com/Radarr/Radarr/wiki/Command-Line-Options

Enable and Start Radarr

systemctl enable radarr.service
systemctl start radarr.service

Check Radarr Status

$ sudo systemctl status radarr
● radarr.service - Radarr Service
   Loaded: loaded (/usr/lib/systemd/system/radarr.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-03-07 10:23:44 PST; 5min ago
 Main PID: 19978 (mono)
    Tasks: 16 (limit: 4915)
   Memory: 114.6M
      CPU: 9.331s
   CGroup: /system.slice/radarr.service
           └─19978 /usr/bin/mono --debug /usr/lib/radarr/Radarr.exe -nobrowser -data=/var/lib/radarr
Mar 07 10:23:44 apollo systemd[1]: Started Radarr Service.

If Radarr does not restart after an update, add the following to your systemd service unit:

ExecStop=-/usr/bin/mono /tmp/radarr_update/Radarr.Update.exe "ps aux | grep Radarr | grep -v grep | awk '{ print $2 }'" /tmp/radarr_update /opt/Radarr/Radarr.exe

Systemd on Linux - Radarr V3

With V3 moving away from mono, you will need to modify the systemd service file for radarr. Specifically, you will need to change ExecStart to the Radarr executable location. Assuming you placed Radarr's executable in the same location as v2, the executable would be /opt/Radarr/Radarr. Remember that linux is case sensitive.

Example:

ExecStart=/opt/Radarr/Radarr -nobrowser -data=/home/radarr/.config/Radarr/

If you are upgrading from V2 and are modifying your existing radarr.service file, ensure you run the command sudo systemctl daemon-reload after you save your changes to your radarr.service file.

Upstart

Using Upstart allows for more advanced features, such as start/stop and automatic restart if it crashes.

Radarr Upstart File

The upstart file should be saved as /etc/init/radarr.conf.

description "Upstart Script to run Radarr as a service on Ubuntu/Debian based systems, as well as others"

#Set user and group for the process
setuid user
setgid group

# Install directory
env DIR=/opt/Radarr

start on runlevel [2345]
stop on runlevel [016]

respawn

exec mono --debug $DIR/Radarr.exe

Start Radarr

sudo start radarr

Init.d Script (Debian/Ubuntu)

You'll need to have already created an radarr user.

Create and edit the radarr init.d script

sudo nano /etc/init.d/radarr

Paste the following, changing applicable variables

        
#! /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

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

# user
RUN_AS=radarr

# 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
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
                log_daemon_msg "$NAME is not running"
                exit 3
            fi

        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
                echo " * $NAME is not running"
                exit 3
            fi
        fi
    ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
    ;;
esac

exit 0

Make it executable

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

Update rc.d

sudo update-rc.d radarr defaults

Create radarr user

useradd radarr
or 
useradd --system radarr (if following the security note in the next section)

Start Radarr

sudo service radarr 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 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 modify 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

Supervisor

[program:Radarr]
command=/usr/bin/mono --verbose /path/to/Radarr.exe -nobrowser
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/radarr.err.log
stdout_logfile=/var/log/supervisor/radarr.out.log
user=<username>