Table of Contents

Ubuntu MythTV Server

Installing

Since Ubuntu 20.04 the mythtv packages now pull in a full desktop suite if you accept the defaults. To avoid this use

--no-install-recommends

for apt, or

--without-recommends

for aptitude. The mythtv-backend-master package is for your first or a standalone backend, to connect a 'slave' backend to an existing system install mythtv-backend instead.

Once installed you will need to run the mythtv-setup program at least once or the server will fail to start.

On Ubuntu 22.04 the server will not start until you create the file /etc/mythtv/additional.args with the following contents:

additional.args
ADDITIONAL_ARGS=""

Remote Frontends

When installing the MythTV 'master' backend Ubuntu package, it asks the question “will you be using MythTV on other computers on the network”. If you answer 'yes' it does not correctly set up MySQL to enable remote connections, you also need to create /etc/mysql/my.cnf with the following text:

my.cnf
[mysqld]
bind-address = *

And restart the MySQL server. This will override the default setting to only listen on the loopback address.

This has been corrected in Ubuntu 22.04, and the override is no longer required.

Sharing Recordings

Add this as a user job to create a directory with sensibly named recordings, then you can share it to access it remotely.

ln -s "%DIR%/%FILE%" "/srv/recordings/$(echo "%TITLE% %SEASON%x%EPISODE% %SUBTITLE% - %STARTTIME% %CHANID%" | sed -e "s_[\':\/\\\"?*]__g").mpg"; find -L /srv/recordings/ -type l -delete

Remember to enable the user job in the default recording rule.

Then you can share the recordings over SMB/CIFS with something like this:

smb.conf
[global]
    log file = /var/log/samba/log.%m
    logging = file
    map to guest = Bad User
    unix extensions = no
 
[recordings]
    path = /srv/recordings
    browseable = yes
    read only = yes
    guest ok = yes
    guest only = yes
    wide links = yes

On Ubuntu 20.04 and later, if you installed samba with the –no-install-recommends option, you also need to install samba-vfs-modules to support the wide links option above.

Handling Recorder Failures

Sometimes you need to reboot the machine to reset the TV tuner card when a recording fails (this doesn't seem to happen as much these days), or you'll just get failed recordings one after the other.

In the “Recording Started” event, add this:

test -f /var/lib/.block-reboot || sudo shutdown -r +1

In the “Recording Started Writing” event, add this:

sleep 1 ; sudo shutdown -c

Add the following to root's crontab:

crontab
@reboot touch /var/lib/.block-reboot ; sleep 600 ; rm /var/lib/.block-reboot

Every time a recording starts, mythtv will now schedule a reboot for one minute in the future (this assumes mythtv can use sudo shutdown without a password). When the recording successfully starts writing to disk, the shutdown will be cancelled (there is a one second delay just to avoid a race condition where the shutdown is cancelled before it is scheduled and the machine reboots anyway!). The cron command attempts to block further reboots for 10 minutes after boot-up, to prevent an endless cycle of reboots when the issue is not the tuner card.

Handling Scheduler Failures

On Ubuntu 20.04 recorder/tuner failures don't really seem to happen any more, instead you seem to occasionally get complete scheduler failures where MythTV thinks there is nothing to record. The Program Guide is full, you can watch Live TV, but the Upcoming Recordings list is just empty even after forcefully re-scheduling. Even running a scheduler test will show that there should be recordings to do, but the actual scheduler disagrees.

The solution seems to be a simple reboot, although my first 20.04 build didn't recover even after that, so fingers crossed that was something I screwed up instead.

Triggering the reboot is the tricky part, as you don't want it rebooting in the middle of a recording, or if the scheduler is busy re-scheduling.

On the server, as the mythtv user (you can use sudo su mythtv to switch users if required), create a script:

nano -w /home/mythtv/check_recordings.sh
check_recordings.sh
#!/usr/bin/bash
 
test -f /var/lib/.block-reboot && exit 0  # if reboots are blocked exit immediately
 
/usr/bin/mythbackend --printsched | /usr/bin/grep -A2 "print list start" | /usr/bin/grep "print list end" &> /dev/null
if [ "$?" != "0" ]; then
    exit 0  # grep exiting 1 means there is at least one recording in the list, so do nothing
fi
 
sleep 180  # give the scheduler chance to schedule programs before we give up
 
/usr/bin/mythbackend --printsched | /usr/bin/grep -A2 "print list start" | /usr/bin/grep "print list end" &> /dev/null
if [ "$?" != "0" ]; then
    exit 0  # grep exiting 1 means there is at least one recording in the list, so do nothing
fi
 
/usr/bin/sudo /usr/sbin/shutdown -r 0 "No recordings in the schedule"

Now add this script to the mythtv user's crontab:

crontab -e
crontab
*/5 * * * * /home/mythtv/check_recordings.sh