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 dont 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?
Thank you very much
I found how to do this and summarized it in my blogpost:
If you want to change the behaviour of
anacron
to execute as soon as the day changes (and not wait till7:30
as is the default behaviour) do the following:and add the last 2 lines (the last line will restart anacron every hour):
The answer by Khaled is not correct. Changing this line will not have an effect if
anacron
is installed (which it is, by default):If anacron exists,
test -x /usr/sbin/anacron
will returntrue
, 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.As of Ubuntu 19.10 (and maybe earlier)
anacron
is run using asystemd
timer.Solution
The way to change
anacron
execution time is with this command:Then modify the line:
What doesn't work
As explained in other answers, editing
/etc/crontab
doesn't help.The part before the operator
||
only tests ifanacron
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 meananacron
isn't installed.Editing
/etc/cron.d/anacron
doesn't help either.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.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|| ...
)You can just change the line:
to read
This will make the execution of daily cron jobs start at midnight.