Backend requires restart to recognize hdhomerun tuners

For discussion of topics specific to MythTV on linux
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

If I reboot my Ubuntu 16.04 system, running MythTV 0.28, start the frontend, and then select Watch TV, nothing happens.

I restart the backend (sudo service mythtv-backend restart) and now the frontend properly works when I select Watch TV.

It's almost like the backend didn't find the hdhomerun tuners the first time it was started after a system boot.

Please advise,

Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

You could just add a sleep 10 (or however long it takes for your HDHR to get
up and running.) That would go in the mythtv-backend.service file. But, if you
modify the one in /lib/systemd/system, then if a new version is released, your
changes will go away. So the proper solution is to put the .service file in /etc/systemd/system
Of course that you'd miss any updates to the official version in /lib. You just need
to be aware of that.

You could do this. Make sure you understand every command below before starting:
  • stop the backend: sudo systemctl stop mythtv-backend
  • sudo --login systemctl edit --full mythtv-backend
  • add: ExecStartPre=sleep 10 (or append: ;sleep 10 to an existing ExecStartPre line)
  • reload: sudo systemctl daemon-reload
  • restart the backend: sudo systemctl start mythtv-backend
That's the OK solution. A better one, in my opinion, is to create a new script
like: /usr/local/bin/hdhomerun.sh with commands to discover the HDHR.
You'd need to customize it for your own system. Run it from the command line
to make sure it works!:

Code: Select all

#!/bin/sh

LOG="/tmp/hdhomerun_config.out"

for i in $(seq 30); do
    hdhomerun_config discover 2>&1 >> $LOG
    if [ $? -eq 0 ]; then
        echo "HD Homerun is up ($(date))." >> $LOG
        exit 0
    fi
    sleep 2
done

echo "Couldn't see the HD Homerun in 30 seconds!" >> $LOG
exit 1
If the comand line test works OK, then add a new service named hdhomerun.service
using: sudo --login systemctl edit --full --force:

Code: Select all

[Unit]
Description=HD Homerun Discovery
Before=mythtv-backend.service
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/hdhomerun.sh

[Install]
WantedBy=default.target
Enable it: sudo systemctl enable hdhomerun

You can then test the service with: sudo systemctl start hdhomerun .

If that works (check the log in /tmp) then add the following line to the new
mythtv-backend.service file created above which will prevent the
backend from starting unless the HDHR can be discovered:

Code: Select all

Requires=hdhomerun.service
The above goes in the [unit] section. E.g after any lines that start with: After=...
You may choose Wants= rather than Requires= so you could watch, but not
record or see LiveTV.)

For either solution, reboot to see if it works. Doing a cold start is an even better test.

If you want to be really safe, instead of putting the changes in /etc/systemd/system,
put them in /run/systemd/system and test by restarting the backend. The changes
will vanish if you do a reboot. Once you're sure they're working, put them in the /etc
location. The --runtime switch is used to put the changes under /run and would be
added to the "edit" systemctl commands above.

EDIT: Changed /usr/local/sbin to /usr/local/bin for consistancy (the script can go wherever you prefer.)
EDIT: Fixed ExecStartPre spelling error and copy instruction, thatks to Glb1945 for spotting them.
EDIT: Updated sequence and prefer systemctl edit instead of cp.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502,

Thanks for the detailed reply!

Questions... this problem occurred after the update to Ubuntu 16.04/MythTV 0.28... it worked fine before with 15.10/MythTV 0.27... and the hdhomerun box is always up and running on my network... are you suggesting that some new delay to externally discover the hdhomerun is now required prior to starting the backend? Any idea why this is happening now?

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

I don't know, but will offer this. In 15.10, the default init program is systemd, but
it is possible/legal to use Upstart, only you'd know which was used. In 16.04 I've
seen everyone using systemd only.

That means the startup sequence may have changed. I shutdown when idle, so
it's a cold boot for me when the BE wakes up to record etc.

On restart, you may notice that the NIC goes down briefly and that the green link
led on the HDHR turns red. For me that meant that the HDHR had to re-aquire it's
link local address. The shell script above logs what happens and I typically see around
4 discovery attempts before the HDHR is available.

Wes had an interesting (and much easier solution) on the forum today. See this:
viewtopic.php?f=36&t=1469&p=7149#p7149 he's caring
for the case where the NIC isn't up, or the addresses aren't assigned in time for
the BE to use them. Personally, I'm trying to do things with systemd just to learn
a bit more about how it works.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502,

I tried your last suggestion, but I don't think that it worked. I created hdhomerun.sh and put it into /usr/local/bin, and hdhomerun.service (but changed the sbin to bin) into /run/systemd/system, and I restarted mythtv-backend, and then I checked the logfile in /tmp/hdhomerun_config.out, and it didn't update the time that the hdhomerun was found. I didn't have the "Requires" in the service file yet.

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

You can use: sudo systemctl start hdhomerun.service to test it and see
the results in the log. Since you didn't do a reboot (which would have
wiped out the /run version), I don't believe it will be executed 'til the Requires
line is added. But taking the cautious approach is a great idea.

I'll assume you tested the shell script from the command line 1st (and don't
forget the daemon reload command.)
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502... I took a look at Wes comments in the other topic, and he points to an SSD boot speed issue... and guess what... I just installed a 1TB SSD... so I may have a similar problem. Not everything has started yet. However, I tried his suggestion, and it didn't work for me. I put "sudo service mysql restart" in the database server wake, but I don't see a new process ID for mysql.

Sudo systemctl and sudo service do the same thing, yes?

Please tell me more about the daemon reload command. There's no man page for it. I haven't done it.

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

sudo systemctl daemon-reload, actually, when I modify .service file and try to start them,
there's a warning to do that command. Could be different between
distributions I guess.

Right, sudo service... and sudo systemctl... may do the same. I haven't
test that much. I assume there a mysql file in your /etc/init.d. In any case
systemctl status mysql<tab> should show you what it's doing.

And, I too have an SSD on my production box.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502... I put hdhomerun.service in /run/systemd/system and tried to start it, and got this error...

$ sudo systemctl start hdhomerun.service
Job for hdhomerun.service failed because the control process exited with error code. See "systemctl status hdhomerun.service" and "journalctl -xe" for details.
alheynneman@Satellite-E55:~/Desktop$ sudo systemctl status hdhomerun.service
● hdhomerun.service - HD Homerun Discovery
Loaded: loaded (/run/systemd/system/hdhomerun.service; disabled; vendor prese
Active: failed (Result: exit-code) since Fri 2016-05-06 20:05:36 PDT; 38s ago
Process: 5905 ExecStart=/usr/local/bin/hdhomerun.sh (code=exited, status=203/E
Main PID: 5905 (code=exited, status=203/EXEC)

May 06 20:05:36 Satellite-E55 systemd[1]: Starting HD Homerun Discovery...
May 06 20:05:36 Satellite-E55 systemd[1]: hdhomerun.service: Main process exited
May 06 20:05:36 Satellite-E55 systemd[1]: Failed to start HD Homerun Discovery.
May 06 20:05:36 Satellite-E55 systemd[1]: hdhomerun.service: Unit entered failed
May 06 20:05:36 Satellite-E55 systemd[1]: hdhomerun.service: Failed with result

Ideas?

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

Run /usr/local/bin/hdhomerun.sh from the command line. Type: echo $?
to print the return code. If the log file already exists execute it as root,
or just remove the file 1st. If that fails just execute the commands that
are in the script yourself. The only 'unusual' one is 'hdhomerun_config discover'
so make sure that's either in a PATH that root knows about or replace
that command with it's full path name, e.g. /usr/al/bin/hdhomerun_config
in /usr/local/bin/hdhomerun.sh.

Note that the .service has been disabled, you'll need to do:
sudo systemctl enable hdhomerun.service once you figure out
why the shell script is failing.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502... the /usr/local/bin/hdhomerun.sh is working fine. Its exit code = 0, and the /tmp/hdhomerun_config.out file correctly shows when the HDH was found. I missed the daemon-reload and enable commands for the service file in /run/systemd/system. Give me the exact order of these commands once I put the test service file into /run.

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

Verify that you have the #!/bin/sh in the beginning of your script.

Enable the service and then do the: sudo systemctl daemon-reload.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502... I verified that I could run /usr/local/bin/hdhomerun.sh successfully. I put homerun.service into /run/systemd/system and removed the executable bit that I had set, and enabled it, daemon-reload, and start, and get the following errors...

$ sudo systemctl start hdhomerun.service
Job for hdhomerun.service failed because the control process exited with error code. See "systemctl status hdhomerun.service" and "journalctl -xe" for details.

$ sudo systemctl status hdhomerun.service
● hdhomerun.service - HD Homerun Discovery
Loaded: loaded (/run/systemd/system/hdhomerun.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2016-05-07 16:19:17 PDT; 8s ago
Process: 6653 ExecStart=/usr/local/bin/hdhomerun.sh (code=exited, status=203/EXEC)
Main PID: 6653 (code=exited, status=203/EXEC)

May 07 16:19:17 Satellite-E55 systemd[1]: Starting HD Homerun Discovery...
May 07 16:19:17 Satellite-E55 systemd[1]: hdhomerun.service: Main process exited, code=exited, status=203/EXEC
May 07 16:19:17 Satellite-E55 systemd[1]: Failed to start HD Homerun Discovery.
May 07 16:19:17 Satellite-E55 systemd[1]: hdhomerun.service: Unit entered failed state.
May 07 16:19:17 Satellite-E55 systemd[1]: hdhomerun.service: Failed with result 'exit-code'.

Cheers, Al
User avatar
bill6502
Developer
Posts: 2323
Joined: Fri Feb 07, 2014 5:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by bill6502 »

Understood.

But what I wanted you to check is If the shebang (#!/bin/sh) is the 1st line in the script.
Without it, it will run OK from the command line, but not from the service. I tested that
myself and without it, a 203 is returned when the service is run. If that doesn't help,
please paste your shell script here.
heynnema
Junior
Posts: 16
Joined: Tue Apr 26, 2016 4:28 pm
United States of America

Re: Backend requires restart to recognize hdhomerun tuners

Post by heynnema »

bill6502... yes, the shebang is there in the /usr/local/bin/hdhomerun.sh... but oh, I just may have discovered what the problem might be... I copied the hdhomerun.sh script from your original reply, and when I pasted it into my editor it added four spaces to the beginning of each of the lines of script, including the shebang, so that it wasn't in column 1, but in column 5! I've removed the spaces and I'll test later and report back.

Cheers, Al
Post Reply