特定日期的Cron表达式

19 cron spring expression quartz-scheduler

我想要一个代表2010年9月6日上午6:00的cron表达式

pax*_*blo 26

原始问题已被标记,cron因此第一部分适用于此.有关Quartz CronTrigger工具的更新答案,请参见下文.


大多数crontabs都不允许您指定年份,因此您可能必须将其放在脚本本身(或脚本/程序的包装器)中.

您可以使用以下内容执行此操作:

if [[ $(date +%Y) != 2010 ]] ; then
    exit
fi
Run Code Online (Sandbox Code Playgroud)

你要找的选项,在早上6点9月6日运行的一年

0 6 6 9 * your_command_goes_here
| | | | |
| | | | +- any day of the week.
| | | +--- 9th month (September).
| | +----- 6th day of the month.
| +------- 6th hour of the day.
+--------- Top of the hour (minutes = 0).
Run Code Online (Sandbox Code Playgroud)

对于Quartz CronTrigger格式,你会看到类似的东西:

0 0 6 6 9 ? 2010
| | | | | |   |
| | | | | |   +- 2010 only.
| | | | | +----- any day of the week.
| | | | +------- 9th month (September).
| | | +--------- 6th day of the month.
| | +----------- 6th hour of the day.
| +------------- Top of the hour (minutes = 0).
+--------------- Top of the minute (seconds = 0).
Run Code Online (Sandbox Code Playgroud)

(详情来自这里).


小智 7

对于一次性工作,'at'命令比cron更适合.

at -f filename 06:00 09/06/2010
Run Code Online (Sandbox Code Playgroud)


小智 5

它只能在 /etc/crontab 中,如下所示:

0 6 6 9 * root test `/bin/date +%Y` == 2010 && <your command>
Run Code Online (Sandbox Code Playgroud)