4

I'm trying to set up a cron job that runs once a week on Sunday. I think I'm supposed to use the "dayofweek" position with it set to 0, but I'm unsure if this overrides the "*" for the day position.

45 2 * * 0 /scripts/backup.sh

Is this correct?

2 Answers 2

10

You are correct - as already reported.

Be aware that jobs scheduled for 02:45 on Sundays won't run on the Sunday in the spring when the clocks leap forward (and jobs scheduled for 01:45 on Sundays will run twice on the Sunday in the autumn (fall) when the clocks fall back). Corollary: don't schedule jobs in the 01:00-03:00 window if you might be running them when the clocks change between winter and summer (standard and daylight saving) time.

Also be aware that you can't run jobs on Sunday 1st of any month by adding a '1' to the third (day) column. If you write:

45 2 1 * 0 /scripts/backup.sh

then the command will be run on the 1st of each month regardless of day, and on Sundays, regardless of the day of the month. Counter-intuitively, the day-of-week column is OR'd with the other explicit conditions rather than AND'd. I learned that the hard way - making a public fool of myself. See the POSIX specification for 'crontab', which requires day 0 = Sunday and does not allow day 7 as a synonym.

3
  • +1 some great info on some gotcha's associated with time changes. Nov 1, 2009 at 19:47
  • My nightly backup ran twice last night because of that, and I filled up my backup disk because of it. Nov 1, 2009 at 20:25
  • Silly thought time: has anyone experimented with the time zone that 'cron' runs in? If the machine has a default time zone of 'US/Pacific', does that mean that all cron-jobs are interpreted using that time zone, even if I work in, say, US/Eastern or GMT0BST? Nov 2, 2009 at 15:44
4

That's correct. That job will run at 2:45am every Sunday.

3
  • Great, thanks! Whenever I add this in Plesk and I enter "0" as the dayofweek, it auto-converts it to "7" ... are 0 and 7 both Sunday? Nov 1, 2009 at 18:29
  • Most cron implementations I have encountered, possibly all, under Linux recognise both 0 and 7 as Sunday. It is always worth checking man 5 crontab on the machine you are working on to be sure. man 5 crontab on my systems suggests that this is an extension that is not present by default on BSD, but that could be old information. Nov 1, 2009 at 18:46
  • 1
    Note that 'day = 7' is not supported by the POSIX standard. Nov 2, 2009 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.