32

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?

0

7 Answers 7

29

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.

2
  • 4
    Don't forget to perform 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
  • This answer is wrong on the Ubuntu part. The command 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.)
    – Jocelyn
    Jun 20, 2020 at 7:05
4

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
1
  • 2
    So, to answer the question of how to change what time it runs, I need to edit the START_HOURS_RANGE, correct?
    – thelr
    Oct 21, 2019 at 12:17
4

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

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

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
  • Very good answer. Note that if you do not have anacron and/or systemd, it will, of course, revert back to normal CRON which is what you have on a "basic" Ubuntu server. Looking at the list of times (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
2

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
0

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.

2
  • You have to be very carefully if you use these command. For example for my RedHat it returns /etc/crontab and /etc/anacrontab files. And if you remove lines with cron.daily entry you just simply turn off execution of all scripts which should be run daily (for me it is logrotate, tmpwatch, cups and so on). Aug 6, 2010 at 9:28
  • 2
    I didn't suggest to remove the line, the question was where to modify the cron.daily time. This is how you locate it (so this was more a "help them help themselves" kind of answer)
    – mvds
    Aug 6, 2010 at 9:34
0

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 )
#
-1

You want to do two things:

  1. Remove the script from cron.daily and put in somewhere else.
  2. Add an entry to your crontab to run the specified script at the specified time:

00 10 * * * /path/to/script

You must log in to answer this question.