跳到主要内容
uencoder

如何Parse Cron Expressions

Understand 和 interpret cron 调度 expressions 到确定当 作业 runs.

问题

你有 cron 表达式和 need 到理解调度它 defines.

前置条件

  • Understanding 的 cron fields
  • Knowledge 的时间-based scheduling

步骤

  1. 1. Learn the five fields

    A standard cron expression has five fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-6, where 0 is Sunday). Each field can be a value, range, list, step (*/5), or wildcard (*).

    输入
    30 2 * * *
    输出
    minute=30, hour=2, every day
  2. 2. Interpret the expression

    Read each field to determine the schedule. '30 2 * * *' means 2:30 AM every day. '*/15 * * * *' means every 15 minutes. '0 9 * * 1-5' means 9:00 AM Monday through Friday. Special strings like @daily provide common shortcuts.

    输入
    0 9 * * 1-5
    输出
    9:00 AM, Monday-Friday
  3. 3. Calculate the next run time

    Given the cron expression and a current time, compute the next time the job will execute. This requires evaluating all five fields together, handling month lengths, and resolving day-of-month vs day-of-week interaction (OR logic in standard cron).

    输入
    0 9 * * 1-5, from: 2023-10-14 10:00
    输出
    next: 2023-10-16 09:00 (Monday)

预期结果

清除 understanding 的 cron 调度和 ability 到计算下一个运行 times.

常见错误

Day of week and day of month interaction
In standard cron, if both day-of-month and day-of-week are restricted (not *), the job runs when EITHER matches (OR logic). Some implementations use AND logic. Check your implementation.
Using 7 for Sunday
Some cron implementations accept 7 for Sunday in addition to 0. Others only accept 0. Check your implementation's convention.

排障指引

  • If the next run time is wrong, verify the day-of-month/day-of-week interaction logic.
  • Step values (*/N) start at the field minimum, not at 1 for all fields.

安全限制

  • Parsing cron expressions is a read-only analysis. No security implications.

相关工具

参考链接