spring 特定日期的 Cron 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3665441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:07:35  来源:igfitidea点击:

Cron expression for particular date

springcronexpressionquartz-scheduler

提问by paxdiablo

I want a cron expression that represents 6th September 2010 6:00 am

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

回答by paxdiablo

Original question was tagged cronso this first section applies to that. See below for an updated answer for the Quartz CronTrigger tool.

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



Most crontabs don't let you specify the year so you'll probably have to put that in the script itself (or a wrapper around the script/program).

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

You can do this with something like:

您可以使用以下方法执行此操作:

if [[ $(date +%Y) != 2010 ]] ; then
    exit
fi

The option you're looking for to run at 6am on September 6 everyyear is

您正在寻找在每年9 月 6 日早上 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).


For the Quartz CronTrigger format, you'd be looking at something like:

对于 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).

(details garnered from here).

(从这里获得的详细信息)。

回答by tbalazs

For one-shot jobs the 'at'command is better suited than cron.

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

at -f filename 06:00 09/06/2010

回答by PajaS

It could be only in /etc/crontab by follows:

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

0 6 6 9 * root test `/bin/date +%Y` == 2010 && <your command>