Cronjob 在预定时间之前运行,可能有什么问题?

sim*_*mer 10 linux crontab

我的 crontab 计划在星期六的第 19-23 天之间进行,我不确定为什么它在 20 日(星期五)运行。有什么猜测吗?

00 21 19-23 * 6 <command>
Run Code Online (Sandbox Code Playgroud)

Mad*_*rin 16

该 Cron 表达式转换为:

At 21:00 on the 19, 20, 21, 22 and 23rd of every month and every Saturday.
Run Code Online (Sandbox Code Playgroud)

所以它明确告诉 cron 在 20 日星期五运行。这是因为:

When the schedule specifies both date and weekday, they're combined with a logical OR,
i.e. the job will run if current_minute == scheduled_minute 
&& current_hour == scheduled_hour && current_month == scheduled_month && 
(current_day == scheduled_date OR current_weekday == scheduled_weekday).
Run Code Online (Sandbox Code Playgroud)

此信息来自这个方便的 Cron 工具:http : //crontab.guru/

要使您的工作在星期六的特定日期运行,您可以使用:

00 21 19-23 * * test $(date +%u) -eq 6 && command
Run Code Online (Sandbox Code Playgroud)

这个解决方案来自crontab 一周中的哪一天 vs. 一个月的哪一天?

  • 注意 `%` 在 cronjobs 中有一个特殊的含义——它分隔命令的标准输入。 (6认同)