5 Cron Expression Mistakes That Wake You Up at 3 AM
Cron looks simple until it pages you at 3 AM. Here are the mistakes I see most, and how to catch them before deploy. 1. Day-of-month and day-of-week are OR, not AND 0 9 1 * 1 does not mean "9:00 on the 1st if
Cron looks simple until it pages you at 3 AM. Here are the mistakes I see most, and how to catch them before deploy.
1. Day-of-month and day-of-week are OR, not AND
0 9 1 * 1 does not mean "9:00 on the 1st if it's a Monday". In standard cron, when both fields are restricted, the job runs when either matches. So it fires on the 1st of every month and on every Monday.
2. Timezones and DST
Cron runs in the server's timezone, and many hosted schedulers use UTC. A job at 30 2 * * * may be skipped or run twice on a DST change in local-time systems. Prefer UTC for anything important.
3. */5 is not "every 5 minutes from now"
*/5 * * * * means minutes 0, 5, 10... of each hour. */7 fires at 0, 7, ..., 56, then again at 0, so there is a 4-minute gap at the top of each hour.
4. Overlapping runs
If a job takes longer than its interval, you get parallel runs. Use a lock (flock) or your scheduler's concurrency policy.
5. Not checking the next runs
The cheapest test is to print the next 10 execution times and read them. I built a Cron Expression Parser that runs in your browser and shows the next runs. Nothing is sent to a server.
Disclosure: I'm the author of that tool. What cron gotcha got you?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.