DEV Community

Cover image for One cron schedule, six syntaxes: the traps between crontab, Quartz, EventBridge, GitHub Actions, Kubernetes and systemd
Thanh Huy
Thanh Huy

Posted on

One cron schedule, six syntaxes: the traps between crontab, Quartz, EventBridge, GitHub Actions, Kubernetes and systemd

"Run this every weekday at 9" is one sentence. Across the schedulers a typical team touches, it is six different strings, and two of the obvious ones are wrong in a way nothing warns you about: the job runs, just on the wrong day or at the wrong hour.

The same schedule, six ways

Platform Weekdays at 09:00
crontab 0 9 * * 1-5
Quartz / Spring 0 0 9 ? * MON-FRI
AWS EventBridge cron(0 9 ? * MON-FRI *)
GitHub Actions 0 9 * * 1-5
Kubernetes CronJob 0 9 * * 1-5
systemd OnCalendar Mon..Fri *-*-* 09:00:00

Quartz adds a seconds field in front. EventBridge wraps the expression in cron( ) and adds a year at the end. Both need ? in one of the two day fields. systemd does not use cron syntax at all.

Trap 1: day 1 is not always Monday

In crontab, GitHub Actions and Kubernetes, 1 in the day-of-week field is Monday. In Quartz and AWS EventBridge, day 1 is Sunday. So 1-5 copied into Quartz means Sunday through Thursday, and the job quietly skips Friday.

The fix is boring and reliable: write day names. MON-FRI means the same thing everywhere that accepts names.

Trap 2: "first Monday" is an OR, not an AND

0 9 1-7 * 1 looks like "the first Monday of the month". When both day fields are set, crontab runs the job if either matches, so this runs on days 1 to 7 and on every Monday.

Some platforms can say "first Monday" directly:

  • Quartz: 0 0 9 ? * 2#1
  • AWS EventBridge: cron(0 9 ? * MON#1 *)
  • systemd: Mon *-*-01..07 09:00

crontab cannot, in one expression. Run it every Monday and exit unless the day of the month is 7 or less. Inside a crontab, % must be escaped:

0 9 * * 1 [ "$(date +\%d)" -le 07 ] && /path/to/job
Enter fullscreen mode Exit fullscreen mode

Trap 3: steps restart every hour

*/45 * * * * reads like "every 45 minutes". Steps restart each hour, so it runs at :00 and :45: 45 minutes apart, then 15. An even 45-minute interval cannot be written as one cron line. If you need a real interval, EventBridge has rate(45 minutes) and systemd has OnUnitActiveSec=45min.

Trap 4: 09:00 in which time zone?

GitHub Actions schedules run in UTC unless you add a timezone: key with an IANA name next to cron. Classic EventBridge rules always run in UTC; EventBridge Scheduler takes a time zone. A UTC 09:00 is 10:00 in Berlin in winter and 11:00 in summer.

On Kubernetes, set spec.timeZone (for example Europe/Berlin) on the CronJob to keep 09:00 local all year.

Daylight saving has one more edge: in a few zones (America/Santiago, America/Havana, Asia/Beirut, Africa/Cairo, the Azores) clocks spring forward at midnight, so 00:00 does not exist that day. crontab and Kubernetes skip a 0 0 * * * run on that date.

Trap 5: small syntax differences

  • crontab accepts 7 for Sunday; a Kubernetes CronJob rejects it. Use 0 or SUN.
  • cron starts the next run whether or not the last one finished. In crontab wrap the command in flock -n; on Kubernetes set concurrencyPolicy: Forbid (skip the new run) or Replace (stop the old one).

A checklist before you ship a schedule

  1. Use day and month names, not numbers.
  2. Write down which time zone the scheduler uses, and set it explicitly where you can.
  3. Look at the next 10 run times, not just the expression.
  4. If both day fields are set, check whether you meant OR.
  5. Decide what happens when a run is still going when the next one is due.

Tools

I kept making these mistakes, so I built a converter that translates a schedule between all six and checks each translation run by run against the original. It says so when a schedule cannot be written in one line on the target: https://opspane.dev/cron/translate/

There are also reference pages for common schedules (every 15 minutes, weekdays at 9, first of the month…) with the same schedule on all six platforms: https://opspane.dev/cron/schedules/

Everything runs in the browser. Which of these has bitten you, and is there one I missed?

Top comments (0)