Cron job runs at the wrong time (timezone and daylight saving)

cron reads its schedule in the server's timezone, which is often UTC and not the one you had in mind. Around a daylight saving change a time can be skipped or happen twice. Check the zone, then give the check the same one.

You need: a Lateping account, shell access to a Linux server, about 5 minutes.

Why the time is wrong

Source: crontab(5).

Check whether it is happening to you

Print the server’s zone and the current time in it and in UTC.

bash
date +"%Z %z"; date; date -u

%Z is the zone abbreviation and %z the offset from UTC. If the first line says UTC +0000 and you expected your local time, that is the cause.

The fix: pick one zone and say it

You have two options. Either one works; mixing them is what causes the surprise.

Write the schedule for the server’s zone. Convert the time once, and put a comment next to it:

crontab
# 02:17 Australia/Melbourne is 15:17 UTC (server runs in UTC)
17 15 * * * /usr/local/bin/backup.sh

Set CRON_TZ in the crontab. crontab(5) says CRON_TZ sets the time zone for the cron table, so the times below it are read in that zone:

crontab
CRON_TZ=Australia/Melbourne
17 2 * * * /usr/local/bin/backup.sh

Not every cron reads CRON_TZ. Check your system’s own crontab(5) before you rely on it.

Avoid the daylight saving gap

If your zone changes its clocks, a job set between about 01:00 and 03:00 can be skipped in spring or run twice in autumn. Two ways out:

  • Move the job to a time that always exists once, for example 04:30.
  • Run the server in UTC, which has no daylight saving, and convert the schedule as above.

Find out when a run did not happen

  1. Create a check in the same zone

    Create a check in Lateping with the same cron expression as the job. Set its timezone to the zone the schedule is read in: the CRON_TZ value, or the server’s zone if you did not set one. A check set in a different zone expects pings at the wrong time and alerts late.

  2. Add the ping

    crontab
    CRON_TZ=Australia/Melbourne
    17 2 * * * /usr/local/bin/backup.sh && curl -fsS -m 10 --retry 5 -o /dev/null https://lateping.com/p/<check-id>
    

    Replace /usr/local/bin/backup.sh with your own command. If the job is skipped because its time never happened, no ping arrives and the check alerts after the grace period.

  3. Check the time in the zone

    Print the time in the zone you gave the check, so you can compare it with the Pings list.

    bash
    TZ=Australia/Melbourne date
    

Notes

  • Set the grace period for the change. A run that moves by an hour at a clock change is an hour off for a check that does not move. Using UTC on both sides avoids it.
  • TZ is not CRON_TZ. crontab(5) does not describe a TZ variable for the schedule. Use CRON_TZ or convert the times.
  • Containers. Many images have no zone data and run in UTC. Run the date check inside the container, not on the host.
  • Zone names. Use names like Australia/Melbourne, not abbreviations like EST, which several zones share.
  • Alerts go by email. On Pro and above they can also go to Slack, Discord or a webhook.

Create a free check at https://app.lateping.com/login.

Next steps

On this page