Cron 表达式解析器
将 cron 表达式解析为人类可读的描述并预览下一个运行时间
Cron 表达式
minute hour day(month) month day(week)
人类可读的描述
At 9:00 AM, Mon, Tue, Wed, Thu, Fri
字段说明
Minute
0
0–59
Hour
9
0–23
Day (month)
*
1–31
Month
*
1–12
Day (week)
1-5
0–6
接下来 10 次计划运行
什么是 Cron 表达式解析?
cron 表达式是由五个空格分隔字段组成的字符串,用于定义周期性任务计划。该格式起源于 Unix cron 守护进程,由 Ken Thompson 于 1979 年为 Version 7 Unix 编写。每个字段代表一个时间单位:分钟、小时、月份中的日期、月份和星期几。解析 cron 表达式意味着将这种紧凑的符号转换为人类可读的描述和一份具体的即将执行时间列表。
标准五字段格式被 Linux 和 macOS 上的 crontab、GitHub Actions 和 GitLab CI 等 CI/CD 平台、AWS(EventBridge)、Google Cloud Scheduler 和 Azure Functions 中的云调度器,以及各主流编程语言的任务调度库所使用。部分系统通过添加秒字段或年份字段将格式扩展为六或七个字段,但 POSIX 定义的五字段布局仍是所有实现共同遵循的基准。
手动解析 cron 表达式容易出错。字段之间的交互会产生不直观的计划:`0 9 1-7 * 1` 并不表示“每月第一周的每个周一”,而是“每月 1 日至 7 日,或任意周一”。cron 解析器通过展开每个字段、应用组合规则并生成任务实际触发的时间戳来消除这种歧义。
为什么使用这个 Cron 解析器?
从配置文件中读取像 `30 */6 1,15 * *` 这样的 cron 表达式并准确知道它何时触发,需要对五个字段进行心算。本解析器即刻完成这项工作。
Cron 解析器使用场景
Cron 表达式语法参考
标准 cron 表达式有五个空格分隔的字段。每个字段支持整数、通配符、范围、列表和步长值。下表展示了每个字段的允许范围和运算符。
| 字段 | 范围 | 运算符 | 说明 |
|---|---|---|---|
| Minute | 0–59 | * , - / | Minute within the hour |
| Hour | 0–23 | * , - / | Hour of the day (24-hour) |
| Day (month) | 1–31 | * , - / | Day of the month |
| Month | 1–12 | * , - / | Month of the year (or JAN–DEC) |
| Day (week) | 0–6 | * , - / | Day of the week (0 = Sunday, or SUN–SAT) |
四个特殊字符控制每个字段中值的匹配方式:
| 字符 | 名称 | 行为 |
|---|---|---|
| * | Wildcard | Matches every possible value in the field. * in the minute field means "every minute." |
| , | List | Separates individual values. 1,15 in the day field means "the 1st and 15th." |
| - | Range | Defines an inclusive range. 1-5 in the day-of-week field means "Monday through Friday." |
| / | Step | Defines an interval. */10 in the minute field means "every 10 minutes." 5/15 means "5, 20, 35, 50." |
以下是覆盖大多数调度需求的常用 cron 表达式:
| 表达式 | 计划说明 |
|---|---|
| * * * * * | Every minute |
| 0 * * * * | Every hour (at minute 0) |
| */15 * * * * | Every 15 minutes |
| 0 9 * * * | Every day at 9:00 AM |
| 0 9 * * 1-5 | Weekdays at 9:00 AM |
| 30 2 * * 0 | Every Sunday at 2:30 AM |
| 0 0 1 * * | First day of every month at midnight |
| 0 0 * * 1 | Every Monday at midnight |
| 0 0 1 1 * | January 1st at midnight (yearly) |
| 0 */6 * * * | Every 6 hours |
| 5,35 * * * * | At minute 5 and 35 of every hour |
| 0 9-17 * * 1-5 | Every hour from 9 AM to 5 PM, weekdays |
代码示例
如何在常用编程语言中解析 cron 表达式并计算下次运行时间:
import cronstrue from 'cronstrue';
// Parse cron to human-readable text
cronstrue.toString('0 9 * * 1-5');
// → "At 09:00 AM, Monday through Friday"
cronstrue.toString('*/15 * * * *');
// → "Every 15 minutes"
// Validate with cron-parser and get next run times
import { parseExpression } from 'cron-parser';
const interval = parseExpression('30 2 * * 0');
console.log(interval.next().toISOString());
// → next Sunday at 02:30 UTC
// Iterate over the next 5 runs
for (let i = 0; i < 5; i++) {
console.log(interval.next().toString());
}from crontab import CronTab
from croniter import croniter
from datetime import datetime
# Parse and describe a cron expression
cron = CronTab('0 9 * * 1-5')
print(cron.next(default_utc=True))
# → seconds until next run
# Get the next 5 run times with croniter
base = datetime.now()
cron_iter = croniter('0 9 * * 1-5', base)
for _ in range(5):
print(cron_iter.get_next(datetime))
# → next 5 weekday 09:00 timestamps
# Check if a specific time matches
print(croniter.match('*/15 * * * *', datetime(2026, 3, 25, 10, 30)))
# → True (minute 30 is divisible by 15)package main
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
)
func main() {
// Parse a standard 5-field cron expression
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
schedule, err := parser.Parse("0 9 * * 1-5")
if err != nil {
panic(err)
}
// Calculate the next 5 run times
now := time.Now()
for i := 0; i < 5; i++ {
now = schedule.Next(now)
fmt.Println(now)
}
// → next 5 weekday 09:00 timestamps
}# List current user's cron jobs crontab -l # Edit cron jobs interactively crontab -e # Add a job: run backup.sh every day at 2:30 AM # (append to crontab via pipe) (crontab -l 2>/dev/null; echo "30 2 * * * /home/user/backup.sh") | crontab - # Check syntax with a dry-run parse (requires cronie or busybox) # The system will reject invalid expressions when saving # View cron logs on systemd-based Linux journalctl -u cron --since "1 hour ago"