[mythtv-users] delaying backend startup until the network is up

Stephen Worthington stephen_agent at jsw.gen.nz
Tue Dec 11 13:24:47 UTC 2018

On Tue, 11 Dec 2018 08:17:33 -0500, you wrote:

>On 12/11/18 2:04 AM, Stephen Worthington wrote:
>> On Mon, 10 Dec 2018 17:03:02 -0500, you wrote:
>>> on my production system I use without issue the systemd override provided by.
>>> sudo systemctl edit mythtv-backend.service
>>> [Unit]
>>> After=NetworkManager-wait-online.service
>>> When I built a test system using the same HDHR tuners but with the mythhdhrrecorder external recorder I used the same overide setup. However, if I boot my test system I get a lot of errors on 2-4 tuners and the errors are related to opening the tuner. This is solved by doing the following after boot:
>>> sudo systemctl stop mythtv-backend
>>> sudo systemctl start mythtv-backend
>>> What is special about mythhdhrrecorder and external recorder blackbox that makes this systemd override not work???
>>> Jim A
>> Looking at the source code for mythhdhrrecorder, I can see that as it
>> starts up, it tries to access https://my.hdhomerun.com/discover as
>> part of its discovery of the HDHR devices.  So it looks like you need
>> full Internet access up (including the name servers working) before
>> that will work.  So I think what is needed is a systemd unit that
>> first does a DNS lookup of my.hdhomerun.com (say once a second), and
>> when that works, does a ping of my.hdhomerun.com and when that works,
>> the unit startup succeeds.  Then you can have mythbackend wait for
>> that unit to be started before it starts.  However, there should be a
>> timeout (say 30 seconds) after which mythbackend gets to start anyway,
>> otherwise if your Internet access is down, you will be unable to start
>> mythbackend.
>> I have never built a systemd unit like that, but I think it would work
>> if you wrote a script (or some Python) to do the DNS lookup and ping,
>> and put it in the ExecStartPre line to be run before the main
>> executable of the unit gets run from the ExecStart line.  The
>> ExecStart line would then need to do nothing.  I am not sure how you
>> are supposed to do nothing in systemd, but running /bin/true should
>> work.
>> So a unit "full-internet.service" like this might work:
>> [Unit]
>> Description=Wait for full Internet service to be available including
>> DNS.
>> After=NetworkManager-wait-online.service
>> [Service]
>> Type=simple
>> ExecStartPre=/usr/bin/python3 /usr/bin/full-internet.py
>> ExecStart=/bin/true
>> [Install]
>> WantedBy=multi-user.target
>> and in your mythtv-backend override file, use:
>> After=full-internet.service
>> The full-internet.py script would handle the DNS lookup, ping, and
>> timeout after 30 seconds.  It might be a good idea if it took the DNS
>> name and timeout values as command line parameters, to make it more
>> generally useful.  I have a script I use for reloading the drivers of
>> an Ethernet card when the connection drops that I could use for a
>> framework to make a full-internet.py script.  It does regular pings to
>> see if the connection is up so it has most of the code I would need.
>> Let me know if you would like me to do this.  I think this is a
>> problem that has cropped up in various forms for years, so it would be
>> worth my writing this if it was going to be useful.
>from time import sleep
>import socket
>for x in range(0, 15):  # try 15 times
>     try:
>         print socket.gethostbyname ('my.hdhomerun.com')
>         str_error = None
>     except Exception as str_error:
>         pass
>     if str_error:
>         sleep(2)  # wait for 2 seconds before trying to fetch the data 
>     else:
>         break
>maybe something like the above for the DNS part?

That looks like it should work, but I was thinking of something
simpler - just use the name in the ping code, so it does an automatic
DNS lookup with every ping attempt.  There should also be code to
allow it to be killed by Ctrl-C without doing a stack dump.  See the
signal handler code in my net-keepalived.py file.

More information about the mythtv-users mailing list