[mythtvnz] HDHomeRun and Mythbackend starting too early

Stephen Worthington stephen_agent at jsw.gen.nz
Sat Nov 4 03:14:43 GMT 2017


On Sat, 4 Nov 2017 14:11:38 +1300, you wrote:

>Thanks guys for all the assistance.  I now have something working.  For
>future reference, he's what I did:
>
>Note:  applies to standard Ubuntu 17.10 install with MythTV 0.29.
>
>1. Find the exact filename of the existing mythbackend.service in the
>/etc/systemd folder for me it was:
>    /etc/systemd/system/mythtv-backend.service
>2. copy this file to /etc/systemd/system/
>3. chown / chmod correct permissions for this new file
>4. edit the [Unit] section of this file to match the following:
>    [Unit]
>    After=network.target mysqld.service NetworkManager-wait-online.service
>pingnetwork.service
>    Wants=pingnetwork.service
>5. systemctl enable mythtv-backend.service

That is exactly the wrong way to go about overriding systemd units. Do
not modify anything in /lib/systemd/system - those files get
overwritten whenever a new version of the package is installed.  And
it is a bad idea to just copy a /lib/systemd/system file to
/etc/systemd/system, as when a new version of a package updates the
/lib/systemd/system file, the changes will not happen in your copy.

There is a proper mechanism for overriding settings a systemd unit
file by using an override file - see the systemd documentation.  Just
the settings you need get overridden, and updates to the original unit
file still work (unless your overrides override those specific
settings).

>
>After these changes, I was hoping that the dependancy on
>*NetworkManager-wait-online.service* would fix the issue.  It did not.
>Mythbackend still starting up just after the network card was initialised,
>but before it is assigned an IP address (i think).  I spent ages trying
>different things without success.
>
>What finally fixed it was adding one more line to the file above in the
>*[Service]* section:
>*   ExecStartPre=/bin/sleep 5*
>
>Yes - i know.  This is an ugly hack.  But it works, and means i can put
>this behind me for now.
>
>Cheers

Yes, indeed it is an ugly hack that will bite you the first time fsck
runs on boot and changes all the timings.  If not sooner if you
install some new software that changes the timings.  It is likely that
NetworkManager-wait-online.service is not enabled (it is not enabled
by default in Mythbuntu 16.04) and that is the reason it did not work
- see below.

Here is a cut and paste of one of my posts on the mythtv-users mailing
list that tells you how to do it properly:

======================================================================
I have just been helping someone else who had exactly this problem -
he upgraded to 16.04 and installed an SSD at the same time, and
mythbackend was starting before his tuners were ready (both HDHR and
internal HVR-2200 tuners).  The use of systemd in 16.04 speeds up the
startup of quite a few things, including mythbackend, and an SSD
speeds everything up.  The package file for mythbackend installs a
systemd /lib/systemd/system/mythtv-backend.service file that does not
take into account HDHR tuners at all - it has nothing in it to make
mythbackend wait until external networking is up and an IP address has
been assigned.  This is the line it does have:

After=mysqld.service network.target

So it only waits for MySQL to be up and minimal networking.  When
mythbackend starts, it immediately checks all the tuners by trying to
tune each one for a short time.  Any tuner that does not respond is
marked as unavailable.  And it never later checks to see if a tuner
has subsequently become available.  So if mythbackend comes up before
an external IP address is assigned, it will try to talk to the HDHR
tuners before any packets can be sent to them, will get no response,
and will never try to talk to them again.

The solution is to add a new After condition that makes systemd wait
for full networking with an external IP address to be up before it
starts mythbackend.

There is a page here:

https://www.mythtv.org/wiki/Systemd_mythbackend_Configuration

that explains how to set up systemd to work with MythTV, but it is
generic rather than directly applying to Mythbuntu 16.04.  The
following is what you need to do in 16.04.

First create a new directory:

sudo mkdir /etc/systemd/system/mythtv-backend.service.d

Then in that directory, create a new file mythtv-backend-override.conf
(the name does not matter but the .conf does).  In that file,
put the following two lines:

[Unit]
After=NetworkManager-wait-online.service

What that does is to tell systemd to wait until the
NetworkManager-wait-online service is running.  I believe that file
needs to be owned by root and have restrictive access rights, so you
may need to run these two commands to ensure that it is set up
correctly after you create it:

sudo chown root:root
/etc/systemd/system/mythtv-backend.service.d/mythtv-backend-override.conf
sudo chmod u=rw,g=r,o=r
/etc/systemd/system/mythtv-backend.service.d/mythtv-backend-override.conf

Those two lines are long enough that they may be wrapped by the email
processing.  Make sure they are one long line each when you paste
them.

Next, run this command:

sudo systemctl status NetworkManager-wait-online.service

If the output of that command says that NetworkManager-wait-online is
not running, then it is likely disabled and needs to be enabled (I
believe it is disabled by default in Ubuntu 16.04).  So enable it:

sudo systemctl enable NetworkManager-wait-online.service

Then you should be able to reboot and mythbackend will be started much
later after the external IP address is available.
======================================================================



More information about the mythtvnz mailing list