Menu

3 cron mistakes that make your job run at the wrong time
πŸ“°

3 cron mistakes that make your job run at the wrong time

DEV Community: beginnersΒ·Avinash KoriΒ·13 days ago
#Fwasr2x7
#devtobeginners#devto#dev#cron#time
Reading 0:00
15s threshold

A cron expression can be perfectly valid and still run at the wrong time. No error, no warning. The job just fires on a day you didn't expect, or hours early, or twice. Here are the three mistakes behind most of these surprises, and how to avoid each one.

Quick refresher: a cron expression has five fields.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0–59) β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0–23) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month (1–31) β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€ month (1–12) β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€ day of week (0–6, Sunday = 0) β”‚ β”‚ β”‚ β”‚ β”‚ 0 9 * * 1-5

Enter fullscreen mode

Exit fullscreen mode

1. Setting both day fields (the OR rule)

What does this mean?

0 0 13 * 5

Enter fullscreen mode

Exit fullscreen mode

Most people read it as "midnight on Friday the 13th". Cron reads it as "midnight on the 13th of every month, or midnight on any Friday".

When both day fields are restricted (neither is * ), classic cron runs the job when either one matches, not both.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.