I have a script in cron.daily that runs at a certain time every morning. I need to change the time that it is run.
How do I change the time cron.daily runs the scripts?
On Red Hat 5 or older , this is controlled in /etc/crontab
.
Newer versions use /etc/anacrontab
. By default, cron.daily
scripts are run at 4:02. Editing /etc/crontab
will modify that time.
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
On Debian/Ubuntu systems, this is controlled in /etc/crontab
as well.
For example; a default Ubuntu 12.04 installation:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
And in either case, you may find more details about what syntax to use here: http://linux.die.net/man/5/crontab or by running man 5 crontab
on almost any Linux system.
sudo systemctl restart cron.service
after you modified this. This is true for systemd
-based systems, such as modern Debian and Ubuntu.
Mar 16, 2017 at 8:57
test -x /usr/sbin/anacron
only tests if anacron is installed, it doesn't execute anything. And if it returns true, then the part after ||
isn't run (lazy operator evaluation). So, this line doesn't do anything when anacron is installed, that's why changing the time doesn't help. (See my own answer for more details.)
in RHEL/CentOS 6 and above
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
Ubuntu 19.10 (and maybe earlier) is using a systemd
timer to run anacron
.
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
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.
systemctl list-units --type=timer
) does show the anacron entry, but no CRON. That being said, more and more systems add systemd timers instead of trying to use CRON. It makes it easier to manage the timer for end users.
Jun 1, 2023 at 14:49
On openSUSE, the crontab looks like:
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
The run-crons
command verifies the timestamps of the files in /var/spool/cron/lastrun
among other things. When the time since the last execution expired, it will run the cron file again.
The time can be influented by touching the file. For example, to set it to 2012-11-17 03:15:
touch -t 201211140315 /var/spool/cron/lastrun/cron.daily
If the line wasn't there, this will not solve anything.
Try finding where cron.daily is mentioned, with
grep -R cron.daily /etc
Then take it from there.
what Jocelyn said was correct. However in the example below. If you change the ||
to &&
it should work just fine
# m h dom mon dow user command
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
You want to do two things:
00 10 * * * /path/to/script