10

I have set a daily anacron job, which runs form /etc/cron.daily. From the /etc/crontab I see it should run daily at 06:25 (or at start-up/reboot).

However when the date changes (00:00) and the computer is up, I don't want anacron to wait till 06:25. If the computer is up I would like to it to run the script as soon as the date changes (00:00). Does it make sense to add a cron job to run anacron @hourly?

5 Answers 5

7

The answer by Khaled is not correct. Changing this line will not have an effect if anacron is installed (which it is, by default):

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

If anacron exists, test -x /usr/sbin/anacron will return true, and prevent the second half of that line from running. The other answer (editing /etc/cron.d/anacron) is correct, since that's the config that will kick off anacron itself.

1
  • This answer is wrong for any systems using systemd. See the correct answer by @Jocelyn from June 20 2020. Apr 27, 2022 at 7:52
7

I found how to do this.

If you want to change the behaviour of anacron to execute as soon as the day changes (and not wait till 7:30 as is the default behaviour) do the following:

gksu gedit /etc/cron.d/anacron

and add the last 2 lines (the last line will restart anacron every hour):

01 0    * * *   root test -x /etc/init.d/anacron && /usr/sbin/invoke-rc.d anacron start >/dev/null
01 0    * * *   root start -q anacron || :
@hourly root start -q anacron || :
3
  • There's a problem with the formatting of your file snippet. I can't tell what you intended, but the "| :" that currently resides outside the code block is almost certainly intended to be somewhere in it. Apr 2, 2013 at 14:55
  • 5
    your link to your blog post seems broken.
    – vaab
    Jan 27, 2016 at 1:25
  • This answer is wrong for any systems using systemd. See the correct answer by @Jocelyn from June 20 2020. Apr 27, 2022 at 7:51
6

As of Ubuntu 19.10 (and maybe earlier) anacron is run using a systemd timer.

Solution

The way to change anacron execution time is with this command:

sudo systemctl edit --full anacron.timer

Then modify the line:

OnCalendar=*-*-* 07..23:30

What doesn't work

As explained in other answers, editing /etc/crontab doesn't help.

25 7    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

The part before the operator || only tests if anacron is installed, it doesn't execute anything. And the part after only get executed if the part before didn't complete successfully (lazy operator evaluation), which then would mean anacron isn't installed.

Editing /etc/cron.d/anacron doesn't help either.

30 7-23 * * *   root    [ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi

The condition if [ ! -d /run/systemd/system ] prevents the script from running when systemd is installed.

The correct way to change the execution time is by editing the systemd timer as explained at the begining.

1
  • 1
    And that, my dear friends, is one of the many reasons one should consider Devuan Linux (or Artix)
    – DrBeco
    Jul 5, 2020 at 15:45
3

as per other answers:

edit the first two columns of /etc/cron.d/anacron to change the time anachron runs.

(/etc/crontab values don't take effect due to the || ...)

1
  • This answer is wrong for any systems using systemd. See the correct answer by @Jocelyn from June 20 2020. Apr 27, 2022 at 7:52
0

You can just change the line:

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

to read

0 0    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

This will make the execution of daily cron jobs start at midnight.

2
  • 3
    -1 This is not related to anacron execution time. If anacron is installed, these lines do nothing. See /etc/cron.d/anacron to change execution time of anacron.
    – vaab
    Jan 27, 2016 at 1:30
  • This answer is wrong for any systems using systemd. See the correct answer by @Jocelyn from June 20 2020. Apr 27, 2022 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .