[mythtv-users] anacron time change after upgrade to Ubuntu 18.04

Jim Abernathy jfabernathy at gmail.com
Wed Nov 7 12:19:29 UTC 2018


On 11/6/18 9:59 AM, Stephen Worthington wrote:
> On Tue, 6 Nov 2018 09:39:12 -0500, you wrote:
>
>> On 11/6/18 7:46 AM, Stephen Worthington wrote:
>>> On Tue, 6 Nov 2018 06:59:31 -0500, you wrote:
>>>
>>>> On my backend I have 2 cron jobs I put into /etc/cron.daily for database
>>>> backup and optimization. The scripts email when done with the date and time.
>>>>
>>>> On Ubuntu 16.04 it ran around 7:35 am. After the 'do-release-upgrade' it
>>>> runs at 12:10 am.
>>>>
>>>> I always wondered why 7:35am with a /etc/crontab entry like the one
>>>> below, but I really can't figure out 12:10 am based on it.
>>>>
>>>> Any ideas?
>>>>
>>>> 25 6    * * *    root    test -x /usr/sbin/anacron || ( cd / &&
>>>> run-parts --report /etc/cron.daily )
>>>>
>>>> Jim A
>>> If you read that crontab code carefully, you will see that what it is
>>> doing is testing to see if an anacron executable is present, and if
>>> so, it does not run the cron.daily tasks from crontab.  As Ubuntu does
>>> have an anacron executable present, those crontab entries do nothing,
>>> and it is left to anacron to run the /etc/cron.daily, /etc/cron.weekly
>>> and /etc/cron.monthly tasks.  The /etc/cron.hourly tasks, however are
>>> run from crontab.
>>>
>>> In Ubuntu 18.04, anacron is now run from systemd.  There are two
>>> systemd units:
>>>
>>> /lib/systemd/system/anacron.service
>>> /lib/systemd/system/anacron.timer
>>>
>>> The main unit that actually runs anacron is anacron.service.  The
>>> anacron.service unit is run when the PC starts up (as long as mains
>>> power is available) and when it wakes up from a sleep mode.  The
>>> anacron.timer unit runs anacron.service every hour, and
>>> anacron.service runs anacron.  Here is what is in those two unit
>>> files:
>>>
>>> root at mypvr:/lib/systemd/system# cat anacron.service
>>> [Unit]
>>> Description=Run anacron jobs
>>> After=time-sync.target
>>> ConditionACPower=true
>>> Documentation=man:anacron man:anacrontab
>>>
>>> [Service]
>>> ExecStart=/usr/sbin/anacron -dsq
>>> IgnoreSIGPIPE=false
>>>
>>> [Install]
>>> WantedBy=multi-user.target
>>> root at mypvr:/lib/systemd/system# cat anacron.timer
>>> [Unit]
>>> Description=Trigger anacron every hour
>>>
>>> [Timer]
>>> OnCalendar=hourly
>>> RandomizedDelaySec=5m
>>> Persistent=true
>>>
>>> [Install]
>>> WantedBy=timers.target
>>>
>>> So in 18.04, you will get the cron.{daily,weekly,monthly} jobs being
>>> run about 5 minutes after the PC starts up, and after that at about 5
>>> past midnight.  The 5 minutes bit is because after anacron it started,
>>> it wait 5 minutes before it does anything.  That is very useful as it
>>> allows you to kill the anacron task immediately after you reboot your
>>> MythTV box, so it will then run anacron at the usual time instead of
>>> immediately.  You only need to kill anacron if the day has changed
>>> since anacron was last run, as anacron uses timestamps stored in files
>>> in /var/spool/anacron to tell it when its jobs were last run and will
>>> not run them again on the same day (or week or month).  The time that
>>> anacron gets run at has a variable extra delay of up to 5 minutes
>>> because of the "RandomizedDelaySec=5m" line in anacron.timer.  So
>>> anacron will run its tasks 5 minutes after boot, and 5-10 minutes
>>> after the hour after the day changes ie 5-10 minutes after midnight.
>>>
>>> In earlier Ubuntu versions, before anacron was run from systemd, it
>>> was run from cron using the file /etc/cron.d/anacron.  That started
>>> anacron at 07:30.  Now, the /etc/cron.d/anacron file will only run
>>> anacron if systemd is not being used.
>>>
>>> I found the change in timing from 07:30 to midnight to be a big
>>> problem.  I have a huge database, and my backup and database check
>>> jobs take over half an hour, during which mythfrontend is not very
>>> usable for long periods.  And if I have more than one recording
>>> happening, I can get bad recordings.  Since I often have multiple
>>> recordings happening at midnight, and I am also likely to be watching
>>> a programme, it was very annoying to have anacron running then.  So I
>>> have changed anacron back to being run at 07:30 only.  I consider that
>>> anacron being run every hour is actually a bug - it does not run the
>>> cron.hourly jobs, so it only needs to be run once a day.
>>>
>>> To change it back to how it used to be, I created a systemd override
>>> file for anacron.timer:
>>>
>>> root at mypvr:/etc/systemd/system# ll -d ana*
>>> drwxr-xr-x 2 root root 4096 Oct 21 01:13 anacron.timer.d/
>>> root at mypvr:/etc/systemd/system# cd anacron.timer.d/
>>> root at mypvr:/etc/systemd/system/anacron.timer.d# ll
>>> total 12
>>> drwxr-xr-x  2 root root 4096 Oct 21 01:13 ./
>>> drwxr-xr-x 27 root root 4096 Oct 23 23:45 ../
>>> -rw-r--r--  1 root root  166 Oct 21 01:13 anacron-timer-override.conf
>>> root at mypvr:/etc/systemd/system/anacron.timer.d# cat
>>> anacron-timer-override.conf
>>> [Unit]
>>> Description=Trigger anacron at 07:30, as happened before the Ubuntu
>>> 18.04 upgrade.
>>>
>>> [Timer]
>>> OnCalendar=
>>> OnCalendar=07:30
>>> RandomizedDelaySec=0s
>>> Persistent=true
>>>
>>> If you want to create an override file like mine, the easy way is to
>>> do this command:
>>>
>>> sudo systemctl edit anacron.timer
>>>
>>> and paste my override file into it.  The systemctl edit command is
>>> only available in newer versions of systemd, but it is very useful as
>>> it sets up the override directory and .conf file with the right
>>> ownership and permissions automatically.
>>>
>>> I have filed a bug report about this problem, but I have not had any
>>> response to it yet:
>>>
>>> https://bugs.launchpad.net/ubuntu/+source/anacron/+bug/1799361
>>
>> This is a great explanation and very helpful.  I've duplicated what you
>> did and will look for the changes to take affect tomorrow.
>>
>> Thanks,
>>
>> Jim A

I think the answer is to remove my database backup and optimize commands 
from /etc/cron.daily and put them back in my $HOME directory.

Then create a new entry in the /etc/crontab that just executes those 
commands when I want them to run.

Jim A




More information about the mythtv-users mailing list