Implementing Sonarr with systemd

So last night I decided to do an apt-get upgrade. Well, I got upgraded alright - to whatever release that switched me from sysvinit to the new systemd.

Yes, yes, I know, there’s a lot of back-and-forth in the linux community regarding systemd, but after taking a day or so to learn it, I can see why it’s being so widely adopted.

Anyway, here’s the sonarr.service info I used (put it in /etc/systemd/system/). It might need a little tweaking to get your paths/username right - but here’s what mine looks like, and it works fine on Ubuntu.

\#--start
[Unit]
Description=Sonarr Daemon

[Service]
User=sonarr
Group=sonarr
Restart=always
RestartSec=5
Type=simple
ExecStart=/usr/bin/mono /opt/NzbDrone/NzbDrone.exe -nobrowser
ExecStop=-/usr/bin/mono /tmp/nzbdrone_update/NzbDrone.Update.exe \`ps aux | grep 
\#  Note the line above. Notice that little "-" sign? That tells systemd to ignore any exit codes.
NzbDrone | grep -v grep | awk '{ print $2 }'\` /tmp/nzbdrone_update /opt/NzbDrone/NzbDrone.exe
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target
\#---end

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.

-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.

-The Restart=always will notice that Sonarr’s pid has disappeared. It will wait for the duration of RestartSec to restart it. If you have a very slow server, you will probably want to increase this time so the update has time to finish.

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 error 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.

1 Like

Hello,

I got a bad message error…
This is still working ?