每年 1 月 1 日执行一次的 Cron 表达式
要让任务每年 1 月 1 日执行一次,使用下面的 Cron 表达式。它的含义是:在00:00, 限每月 1 号, 仅于一月份。
0 0 1 1 * 在00:00, 限每月 1 号, 仅于一月份 逐字段解释
| 字段 | 值 | 含义 |
|---|---|---|
| 分钟 | 0 | 等于 0 |
| 小时 | 0 | 等于 0 |
| 日 | 1 | 等于 1 |
| 月 | 1 | 等于 1 |
| 星期 | * | 任意值 |
要让任务每年 1 月 1 日执行一次,使用下面的 Cron 表达式。它的含义是:在00:00, 限每月 1 号, 仅于一月份。
0 0 1 1 * 在00:00, 限每月 1 号, 仅于一月份 | 字段 | 值 | 含义 |
|---|---|---|
| 分钟 | 0 | 等于 0 |
| 小时 | 0 | 等于 0 |
| 日 | 1 | 等于 1 |
| 月 | 1 | 等于 1 |
| 星期 | * | 任意值 |
Linux crontab
# crontab -e
0 0 1 1 * /path/to/script.sh >> /var/log/job.log 2>&1 GitHub Actions
on:
schedule:
- cron: '0 0 1 1 *' # UTC 时间 Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: my-job
spec:
schedule: "0 0 1 1 *"
timeZone: "Asia/Shanghai"
jobTemplate:
spec:
template:
spec:
containers:
- name: job
image: busybox
command: ["date"]
restartPolicy: OnFailure Spring @Scheduled
// Spring 多一个“秒”字段,放在最前面
@Scheduled(cron = "0 0 0 1 1 *")
public void run() { /* ... */ } 注意:Cron 按运行环境的时区执行。服务器和 GitHub Actions 通常是 UTC,Kubernetes 可以用 timeZone 指定。