Running sonarr on FreeBSD to keep track of television that we watch works great, but sometime mono gives up the ghost (exited on signal 6) for no easily apparent reason. As this seems to be some sort of fatal error within the mono framework, it is probably a bit beyond my actual understanding.

However, it’s a somewhat infrequent (2–3 times a week?) occurrence. So maybe we can just set a monitor daemon to keep an eye on sonarr & restart it as need be. Or at least that’s the solution I have gone with.

(Related: this solution should totally work for FreeNAS users as well, as this is all done in a jail on my FreeBSD server)

Need to create the actual monitor script (/root/.local/bin/sonarr_monitor in my case):

#!/bin/sh

while :
do
        service sonarr status > /dev/null 2>&1
        if [ $? != 0 ]
        then
                echo "Sonarr went down. Trying to restart..."
                service sonarr restart
        fi
        sleep 15
done

Now we need an rc script (/usr/local/etc/rc.d/sonarr_reloader) to enable & start the service on boot of the jail:

#!/bin/sh

# PROVIDE: sonarr_reloader
# REQUIRE: sonarr

. /etc/rc.subr

name="sonarr_reloader"
rcvar=sonarr_reloader_enable

: ${sonarr_reloader_enable="NO"}

pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-f -P ${pidfile} -r /root/.local/bin/sonarr_monitor"

load_rc_config $name
run_rc_command "$1"

Once those are in place, we just need to enable the script in /etc/rc.conf (or I often use /etc/rc.conf.local instead for non-critical services):
sysrc sonarr_reloader_enable="YES"
…and now we can start the service!
service sonarr_reloader start
Voíla! We now have a daemonized monitor script that will poll sonarr through the service framework, and restart the service if it looks like it has died.

And while I haven’t tried it yet, since we’re throwing the -f flag to daemon, we shouldn’t even need to (a) pipe the initial service sonarr status output to /dev/null, nor (b) bother with the echo output line, as it will be silenced (I used this originally before I learned how to daemonize the script).


Orrrrrrr…you could just install sysutils/daemontools from the ports collection, which apparently can do all that for you.

I may or may not have discovered this a couple hours later when I was reading up on the daemon command a bit more. Doh!

 

Leave a Reply

Your email address will not be published. Required fields are marked *