Cron 表达式入门:5 字段与常见坑
字段语义、Vixie cron 的日/星期或逻辑、时区问题与验证方法。
Cron is still the scheduling language of servers, CI pipelines and Kubernetes CronJobs — and still the source of countless why did it run at midnight? moments. The syntax fits in one screen once the fields click.
The five fields, left to right
- minute 0-59
- hour 0-23
- day of month 1-31
- month 1-12
- day of week 0-7 (0 and 7 are both Sunday)
An asterisk means every value, a list picks several, a range spans, and a step (*/5) filters. So */15 9-17 * * 1-5 reads: every 15 minutes, from 9 to 17, every day, every month, Monday to Friday.
The one rule that trips everyone
When both day-of-month and day-of-week are restricted, standard Vixie cron runs the job on days matching either — not both. That surprise is behind most phantom schedules. Verify any expression in the Cron Expression Parser, which explains the rule and previews upcoming run times.
Shorthands and modern dialects
@hourly,@daily,@weekly,@monthly,@yearly— readable equivalents of common patterns.- Quartz uses 6 fields (adds seconds) and different day names — a frequent porting bug.
Time zones: the silent schedule killer
Cron evaluates in the server time zone. A container running UTC shifts every 0 9 * * * job by hours versus your wall clock. When coordinating schedules across systems, convert the run times with the Unix Timestamp Converter and log the actual epoch of each execution.
Before pasting into crontab, a quick pass through the parser catches reversed fields, off-by-one day names and impossible dates like February 30.
相关指南
文中提到的工具
Cron Expression Parser
Parse a 5-field cron expression into plain English, validate every field, and preview the next scheduled run times. All offline — supports *, ranges, steps and lists.
Open tool →Unix Timestamp Converter
Convert Unix timestamps (seconds or milliseconds) to human dates in local time, UTC and ISO — and dates back to epoch seconds. Copyable, live “now” button.
Open tool →