20

Last weekend we had to change the time from 02:00 to 03:00.

Question: What would happen if there was a cronjob at 02:30?

crond is a very old solution for scheduling, it should probably handle it, but don't know how.

Anthon
  • 78,313
  • 42
  • 165
  • 222
LoukiosValentine79
  • 1,479
  • 3
  • 21
  • 43

2 Answers2

22

It probably depends on your cron implemenation, but the popular Vixie cron states in the manual:

cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute.

and

Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of daylight savings time. If the time has moved forwards, those jobs which would have run in the time that was skipped will be run soon after the change. Conversely, if the time has moved backwards by less than 3 hours, those jobs that fall into the repeated time will not be re-run.

Only jobs that run at a particular time (not specified as @hourly, nor with '*' in the hour or minute specifier) are affected. Jobs which are specified with wildcards are run based on the new time immediately.

Since the DST change was less than 3 hours, your program would run shortly after 3:00 AM

I am not sure if this is Vixie cron specific behaviour, I seem to recall this is how my PDP-11 worked as well back in the 80s but I am not sure.

Anthon
  • 78,313
  • 42
  • 165
  • 222
  • 2
    Wow, knowledge from the 80s - Hats off! – Isaac Mar 29 '16 at 13:25
  • rule of thumb: once a year your crons in that hour range won't run. once a year your crons will run twice. corollary: this isn't always true. related: stop using local time zones! – tedder42 Apr 04 '16 at 17:46
  • @tedder42 please provide references, your claims contradict both the documentation and the actual behaviour of cron. – Anthon Apr 04 '16 at 18:37
  • [not true, Anthon. That's the joy of different implementations.](http://stackoverflow.com/a/13198787/659298) [Here's a code walkthrough of one implementation](http://askubuntu.com/a/3814/69983). [Here's an anecdotal case that is worse than I mentioned](http://blog.endpoint.com/2013/04/avoid-200-and-300-am-cron-jobs.html). – tedder42 Apr 04 '16 at 18:40
  • That anecdotal case references 2am and 3am and has no relevance to the question (which refers to 2:30) pm. Your references might be interesting (old) behaviour, it doesn't align with my experience. And running a server without timezone has its own problems. – Anthon Apr 04 '16 at 20:16
  • There's significant drift between "contradict both the documentation and actual behavior" and your latter "doesn't align with my experience" concession. – tedder42 Apr 04 '16 at 23:34
  • 1
    The quote from Vixie cron man given by @Anthon is consistent with the behaviour of cron on Ubuntu 20.04 LTS (based on Debian) in 2022. (Debian still uses Vixie cron 3.0pl1 [patched], although their [wiki](https://wiki.debian.org/cron) mentions a plan to migrate to cronie.) TLDR: cron does the right thing! – gogoud Oct 30 '22 at 06:58
3

The best solution is to have your computer running with the hardware clock set to UTC, (Coordinated Universal Time), AKA GMT or Zulu Time and only change the way that the time is displayed by setting the local time zone to allow the Day Light Saving time to take over.

On SUSE Linux, and probably most others, if the Hardware Clock System -> Environment -> Clock -> HWCLOCK is set to UTC with the -u flag then and your time zone is set to where you are the system will automatically display local times with DST corrections for you.

This has a number of advantages:

  • You will never have to manually adjust the clock for DST changes again
  • CRON tabs will be stored in and executed at UTC times
  • If a file, e.g. a log, is written at 01:30 before the end of DST and another at 01:20 after the end of DST that file will still be "Newer" than the other one because the file timestamps will be UTC and just translated for display based on the current time zone at display time.

For more information type man hwclock in a terminal.

Steve Barnes
  • 179
  • 5