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

James Abernathy jfabernathy at gmail.com
Tue Dec 18 01:30:18 UTC 2018

On 12/17/18 7:44 PM, Stephen Worthington wrote:
> On Mon, 17 Dec 2018 16:31:47 -0500, you wrote:
>> On 12/11/18 8:24 AM, Stephen Worthington wrote:
>>> 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.
>>>> ---------------------
>> I created a delaying mechanism so mythtv backend would not run until
>> mythhdhrrecorder could "discover" the HDHR tuners requiring full
>> internet access.  As Stephen mentioned I added a full-internet.service
>> and a script. This is not elegant but with my poor programming skills
>> it's the best I could do for now.
>> I put the following service file in /etc/systemd/system:
>> "full-internet.service"
>> Description=Wait for full internet service to be available including DNS
>> After=NetworkManager-wait-online.service
>> [Service]
>> Type=simple
>> ExecStartPre=/home/jim/delay-backend-till-network-up.py
>> ExecStart=/bin/true
>> [Install]
>> WantedBy=multi-user.target
>> ------------------------------------
>> Then I put this script in my home directory:
>> "delay-backend-till-network-up.py"
>> #!/usr/bin/env python3
>> # -*- coding: UTF-8 -*
>> import os, sys, socket, struct, select, time, signal
>> import syslog
>> syslog.syslog("delaying mythtv-backend - starting")
>> def check_ping():
>>      hostname = "my.hdhomerun.com"
>>      response = os.system("ping -c 1 " + hostname)
>>      return response
>> time.sleep(30)
>> if check_ping() == 0:
>>          syslog.syslog("ping worked after 30 sec sleep")
>> syslog.syslog("delaying mythtv-backend - ending")
>> ------------------------------------
>> I changed the override.conf file in
>> /etc/systemd/system/mythtv-backend.service.d to :
>> [Unit]
>> After=full-internet.service
>> ---------------------
>> Don't forget the enable the full-internet.service: sudo systemctl enable
>> full-internet
>> Make delay-backend-till-network-up.py executable.
>> This basically sleep for 30 seconds and then pings my.hdhomerun.com.
>> works for me so I can continue testing.
>> Jim A
> Last night I wrote a "wait-until-pingable.py" program that will wait
> until a specified address can be looked up and pinged, or a specified
> timeout is reached:
> http://www.jsw.gen.nz/mythtv/wait-until-pingable.py
> Use the -h option from the command line to get help on its options.
> Warning: This is alpha software - it has not been tested in real life
> yet, just from my command line.  It does not support IPv6 yet, and I
> also want to add a log message that tells how long it needed to wait
> for its first ping response.  But it should work well enough to do the
> job for now.
> I would recommend putting it in /usr/local/bin and using it by
> changing the ExecStartPre line in the full-internet.service file to:
> ExecStartPre=/usr/local/bin/wait-until-pingable.py my.hdhomerun.com 30
> I will update the file on my web server when I get IPv6 working and
> add the log message.

For some reason I can't run this without a syntax error.  It doesn't 
like errno and msg.

Jim A

> _______________________________________________
> mythtv-users mailing list
> mythtv-users at mythtv.org
> http://lists.mythtv.org/mailman/listinfo/mythtv-users
> http://wiki.mythtv.org/Mailing_List_etiquette
> MythTV Forums: https://forum.mythtv.org

More information about the mythtv-users mailing list