How to change the time anacron runs.

Well this one took me a while to figure out, so I thought I'd blog about it in case I could save someone else some time. Anacron is installed on desktop / laptop orientated distributions as they're often switched off. It basically makes sure the daily, weekly and monthly cron jobs are run by checking the time they were last run, and running them if they weren't run in the last day, 7 days or 30 days respectively. It will also check these when the machine is rebooted. So it makes sense on machines that are rebooted frequently.

However I have a server where it is also installed, alongside cron, and I wanted to change the time logwatch ran every day. My first attempt was to simply change the times in /etc/crontab, which contained lines like this
25 00    * * *    root    test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.daily )
Changing the time was ineffective, although I gather that if I'd removed anacron (or specifically the file /usr/sbin/anacron ) then this approach would have worked.

My research let me next to the /var/spool/anacron directory where there are three timestamp files cron.daily, cron.weekly and cron.monthly. I experimented with changing the time on these to fool anacron into running earlier in the day. This was also ineffective.

Hmm. Finally I found the solution, which kinda makes sense, but is well hidden. In /etc/cron.d/ there is a file which runs anacron containing the line
30 23    * * *   root    test -x /etc/init.d/anacron && /usr/sbin/invoke-rc.d anacron start >/dev/nul

Changing the time on this will make anacron run at your chosen time, and you'll have your logwatch report before breakfast.

 

3 thoughts on “How to change the time anacron runs.”

  1. Timestamp looks to be kept here:

    user@host:/var/spool/anacron$ sudo cat cron.weekly
    20200131

    I think that modifying date contained in the file, is doing the trick.

  2. I'm not sure at the moment, but that looks like the stamp from the last time anacron ran. So when you boot up, it will know if it has to run again (eg if the stamp is > 24 hours for cron.daily).
    My understanding is that regular cron works well for servers which are on 24/7, but anacron is more useful on desktops which are switched on and off.

Leave a Comment