[mythtv-users] optimize before shut down

Stephen Worthington stephen_agent at jsw.gen.nz
Wed Apr 24 05:19:14 UTC 2019

On Tue, 23 Apr 2019 22:28:26 -0400, you wrote:

>On Tue, Apr 23, 2019 at 8:44 PM jam at tigger.ws <jam at tigger.ws> wrote:
>> On 24 Apr 2019, at 4:24 am, mythtv-users-request at mythtv.org wrote:
>> Having the script in /etc/cron.daily doesn't seem to be functional, I'd
>> like to have the database optimize itself just before the nightly
>> auto-shutdown. What is a good way to accomplish this?  It's mythtv fixes
>> 0.29 on Ubuntu desktop 18.04.  TIA  Daryl
>> You say having the script in /etc/cron.daily is not functional, but you
>> haven't entirely explained what you mean by that.
>> I’ve run mythtv for many years
>> I’ve some thousands of videos
>> I’ve some tens of recordings and some 100s of deleted
>> I’ve never run optimise.
>> I ran optimise and it completed in 15 odd seconds, so why not have
>>  an ordinary reboot cron entry?
>> James
>> ______________________________________
>After a few days of normal use, I start seeing momentary freeze ups in
>playback, while the optimize script is in cron.daily. Running it from the
>terminal clears things up, doing it automatically, after the days
>recordings would be ideal.

If the optimize_db (database optimise/repair) or mythtv-database
(database backup) scripts are allowed to run at the same time as you
are playing something in mythfrontend, then you can get significant
slowdown in the response times from mythfrontend, and in the worst
case you can get playback pauses.  It depends on the size of the
database, and what is stored where.  If you are trying to play a
recording that is on the same disk as the database, you will get the
worst effects.  With a huge database like mine, you can get problems
with recordings as well.  If I have more than one recording running
while those scripts are running, I will get damaged recordings.  I am
not sure of the exact mechanism, but it may be due to writes to the
database (especially to the recordedseek table) not working properly.

Since my MythTV box is on 24x7, I have scheduled the scripts to run at
a time when I will never be watching anything and when it is very
unlikely that there will be more than one recording happening.  In
Ubuntu 18.04, that required that I fix a bug that changed when anacron
was running things - they started being run just after midnight, which
is often a time when I have several recordings happening.  Anacron
used to be run at a much better time, 07:30, in older versions of

Originally, anacron was run from the /etc/cron.d/anacron script.  That
script still exists and is still run at 07:30, but does nothing as it
now has a check to see if systemd is being used and if so it exits
without running anacron.  In Ubuntu 18.04, anacron is now run from
these two systemd units:


The anacron.timer unit runs the anacron.service unit once per hour,
with a randomised 5 minute delay.  The anacron program itself does a 5
minute delay before it does anything, and it also checks to see if it
has already been run today before it runs the daily scripts (and
similarly for the weekly and monthly scripts).  It does that by
storing the last time it ran those scripts in files in
/var/spool/anacron (cron.daily, cron.weekly, cron.monthly).

The net result is that in Ubuntu 18.04 (and later???) anacron normally
gets run once per hour and it will normally only do its daily tasks
the first time it is run on any given day.  If the PC is running 24x7,
that means anacron is run at a time randomly selected within 5 minutes
after midnight, and 5 minutes after it is started it will run the
daily scripts in /etc/cron.daily.  If your PC is not on 24x7, the
first time it is booted after midnight, anacron will be started after
a randomised delay of up to 5 minutes after it boots, and it will then
delay 5 minutes before it runs the daily scripts.  That is not a good
way to run optimize_db and mythtv-database, as it means they will
normally be run while a recording is happening, or while you are
trying to watch a recording.

To make anacron work the same way it used to in earlier versions of
Ubuntu, you need to override the anacron.timer file with a systemd
override file.  The easy way to do that now is to do this command:

sudo systemctl edit anacron.timer

which will bring up your system editor to create a file in
/etc/systemd/system/anacron.timer.d.  In that file, put something like
what I have:

Description=Trigger anacron at 09:30.


I have chosen to run anacron at 09:30 daily, as that is an even better
time for me than the original 07:30.  You can choose any time you like
in the above settings, and anacron will be run at that exact time.
When anacron runs, it will delay doing anything by its usual 5

With this sort of fix in place, if your system is being shut down
between recordings, anacron will still be run on the first boot up
that happens after the time you select.  It will wait 5 minutes, then
actually run all the scripts, which is not good as by then MythTV will
likely be busy with a recording or playback.  During that 5 minute
delay, you can just kill anacron if you want to.  Using this command
should do that:

sudo pkill anacron

If you kill anacron before it starts running scripts, there are no bad
side effects.  When it is next run, it will run all the scripts that
should have been previously run.  So that is what I normally do if I
ever have to reboot my MythTV box after midnight and before anacron's
scheduled run time at 09:30.

If anacron has already started running scripts, then if you kill it,
you will also kill all the scripts it was running, and if they were in
the middle of doing something, you can get problems.  At the very
least, there can be temporary files left behind, so it is best to
avoid that unless you know how to check on what the scripts were doing
and manually clean up any messes that got left behind.

If you do not have a good time to allow anacron to automatically run
optimize_db and mythtv-database, you could look at using my "gaps"


to work out when you have a gap in recordings that is long enough. But
if you are shutting down the PC when it is not in use by MythTV, then
probably the best way to run optimize_db and mythtv-database is to use
some variant of Peter Bennett's mythshutdown.sh script, and remove
them from the cron.daily and cron.weekly tasks completely.

More information about the mythtv-users mailing list