brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a cron job at 02:30 can be skipped or run twice when the clocks change

Cron
~/tips/tip-cron-dst-skipped-hour

30 2 * * * /usr/local/bin/nightly-backup.sh

Explains why jobs at 02:30 can be skipped or run twice when the clocks change, and how to avoid it.

I live in the Netherlands, where the clocks change twice a year, and that is exactly what an early-morning cron job does not like.

The cronie manual for crontab(5) spells it out. Non-existent times, such as the missing hours during the daylight saving time conversion, never match, so jobs scheduled in those missing times are not run. Times that occur more than once, which is the case when the clocks go back, make matching jobs run twice.

In the Netherlands the clocks jump from 02:00 straight to 03:00 in spring, and in autumn the hour from 02:00 to 03:00 happens twice. A job like this one is in the danger zone on both nights:

30 2 * * * /usr/local/bin/nightly-backup.sh

In spring 02:30 does not exist that night, so the backup does not run. In autumn 02:30 exists twice, so it can run twice. The fix is simple: move the job to a time that always exists exactly once, for example 04:30.

30 4 * * * /usr/local/bin/nightly-backup.sh

If the job really must be at 02:30 local time, make it safe to run twice. A lock such as flock -n, or a job that notices its work is already done, is a better answer than hoping for the best.

Another option is to set the time zone of the crontab itself. The manual describes CRON_TZ, which sets a time zone for the cron table, and says you should enter times in that zone. Pointing it at UTC removes the gap, at the cost of the job moving by an hour against the wall clock when the clocks change:

CRON_TZ=UTC
30 1 * * * /usr/local/bin/nightly-backup.sh

The caveat: this is what the cronie page says. Other cron implementations may handle the change differently, so check man 5 crontab on the machine that matters.

Further reading: crontab(5) manual (cronie)

Going deeper: Guide: find out why a Linux service failed, with journalctl

More Cron tips echo 'systemctl restart myapp' | at 03:00 tomorrow 55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /usr/local/bin/month-end.sh EDITOR=nano crontab -e 30 6 * * mon /usr/local/bin/weekly-report.sh @daily /usr/local/bin/backup.sh All Cron tips (32) All topics

Link