OK, Here’s the cleanest and best job we can do at present with systemd (for reasons that will be outlined at the end of this post):
#--start of sonarr.service (which I locate in /etc/systemd/system):
[Unit]
Description=Sonarr Daemon
[Service]
User=sonarr
Group=sonarr
Restart=always
RestartSec=5
Type=simple
ExecStart=/usr/bin/mono /opt/NzbDrone/NzbDrone.exe -nobrowser
# Note: The below ExecStop line is supposed to be on ONE LINE.
# The forum automatically line-breaks it. Also, if you opt to NOT
# use the built-in Sonarr updater and instead plan to use a package
# manager, delete or disable the ExecStop line by commenting it out.
ExecStop=-/usr/bin/mono /tmp/nzbdrone_update/NzbDrone.Update.exe \`ps aux | grep
NzbDrone | grep -v grep | awk '{ print $2 }'\` /tmp/nzbdrone_update /opt/NzbDrone/NzbDrone.exe
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target
#---end of sonarr.service
After putting the file in /etc/systemd/system, run the following commands:
systemctl daemon-reload
systemctl enable sonarr
systemctl start sonarr
That will have it load at startup and set the proper runlevels, etc.
Now, here’s a few explanations for why I did things this way.
-All you really need is a service type of ‘simple’. Nothing exotic is required. Notice the ExecStop command line. It has a minus sign before the command. That means to simply ignore any error codes it may run into. It’s probably not necessary, but if the executable to upgrade Sonarr doesn’t currently exist in /tmp, then you MIGHT get some kind of service failure. I figure it’s better safe than sorry.
-The Sonarr updater takes 3 main args, first is the pid of the current sonarr process (which it will kill) and then the source and target directories of the update. Once Sonarr kills the process, the ExecStop command line argument runs - which updates Sonarr. Much thanks to markus for helping me out with the updater on irc
-The Restart=always will notice that Sonarr’s pid has disappeared and attempt to execute the updater. It will wait for the duration of RestartSec to restart it afterwards, though the updater itself respawns Sonarr automatically anyway (so you’re covered either way if for some reason the update fails). NOTE: If you have a very slow server, you will probably want to increase this time so the update has time to finish. 30 seconds is probably a VERY safe number, even if it means waiting 30 seconds for Sonarr to come back to life.
There is ONE caveat to this whole thing. While implenting Sonarr under systemd this way will preserve all functionality, there is ONE function that won’t work. The “Power Off” icon under the system tab will do the same thing as the restart icon. Why? Because systemd has no exit code to read. The updater and the NzbDrone.exe binary under mono will ALWAYS exit with an error code of 0, so the systemd service has no way to determine exactly how Sonarr has been terminated. Perhaps in the future the Sonarr team can implement exit codes for both the stop and restart functions. That would allow us to further tailor Sonarr’s behavior under systemd.
But for now, this is the best that we can do - which is still pretty good. I can live without the power-off button, since i can just do a systemctl stop sonarr anyway.
One last item of note: If you plan on using the updating method further down in this thread (using sonarr-update.timer and sonarr-update.service), make sure you remove the ExecStop= line, as it is no longer necessary - and COULD potentially downgrade you if you have a stale copy sitting in /tmp.