Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
+1 some great info on some gotcha's associated with time changes. –  3dinfluence Nov 1 '09 at 19:47
    
My nightly backup ran twice last night because of that, and I filled up my backup disk because of it. –  Paul Tomblin Nov 1 '09 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? –  Jonathan Leffler Nov 2 '09 at 15:44

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

share|improve this answer
    
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? –  typeoneerror Nov 1 '09 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. –  David Spillett Nov 1 '09 at 18:46
    
Note that 'day = 7' is not supported by the POSIX standard. –  Jonathan Leffler Nov 2 '09 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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