`
EdgenHuang
  • 浏览: 109492 次
  • 性别: Icon_minigender_1
  • 来自: 福建泉州
社区版块
存档分类
最新评论

Cron format

    博客分类:
  • Java
阅读更多

Cron format

A cron specification allows the complex declaration of job scheduling which is tied to the system clock and calendar.

 

For those unfamiliar with "cron", this means being able to create a firing schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".

 

A "Cron-Expression" is a string comprised of 6 or 7 fields separated by white space. The 6 mandatory and 1 optional fields are as follows:

Field Name Allowed Values Allowed Special Characters
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? / L W C
Month 1-12 or JAN-DEC , - * /
Day-of-Week 1-7 or SUN-SAT , - * ? / L C #
Year (Optional)      empty, 1970-2099    , - * /

 

The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".

 

The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.

 

The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".

 

The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".

 

The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.

 

The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

 

The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.

 

The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".

 

The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

 

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".

 

The legal characters and the names of months and days of the week are not case sensitive.

Examples

Expression Meaning
"0 0 12 * * ?" Fire at 12pm (noon) every day
"0 15 10 ? * *" Fire at 10:15am every day
"0 15 10 * * ?" Fire at 10:15am every day
"0 15 10 * * ? *" Fire at 10:15am every day
"0 15 10 * * ? 2005" Fire at 10:15am every day during the year 2005
"0 * 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?" Fire every five minutes starting at 2pm and ending at 2:55pm, every day
"0 0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0 0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March
"0 15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?" Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?" Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005" Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3" Fire at 10:15am on the third Friday of every month

 

Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!

NOTES:

  • Support for the features described for the 'C' character is not complete.
  • Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
  • Be careful when setting fire times between midnight and 1:00 AM - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.
分享到:
评论

相关推荐

    cron-parser:cron解析器

    cron解析器 具有时区支持的cron表达式解析库。 例子: use chrono::{TimeZone, Utc};use chrono_tz::Europe::Lisbon;use cron_parser::parse;fn main() { if let Ok(next) = parse("*/5 * * * *", &Utc::now()) { ...

    Cron定时任务Linux总结.docx

    * 0 * * * /usr/bin/curl -u username:password -H 'Content-Type: application/json' -d '{"query": {"range": {"@timestamp": {"lt": "now-7d", "format": "epoch_millis"}}}}' -X POST "http://127.0.0.1:9200/*...

    cron-parser:用于解析crontab指令的Node.js库

    cron解析器 Node.js库,用于解析和处理crontab指令。 它包括对时区和DST转换的支持。 兼容性节点> = 0.8 打字稿<= 4.2 设置 npm install cron-parser 支持格式 * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ |...

    erl-cron:用Erlang编写的Cron实现

    application:start(erl_cron).erl_cron_server:add_job({"*/2 * * * *", fun () -> io:format("job completed!~n", []) end}). 2添加计划作业(MFA)。 application:start(erl_cron).erl_cron_server:add_job({"*/2...

    java cron 表达式 java cron 表达式 java cron 表达式

    = null) { formatTimeStr = sdf.format(date); } return formatTimeStr; } public static String getWeekCron(String executionCycle, String startTime) { String[] split = startTime.split(":"); String h = ...

    CronUI:基于python-flask的Cron Web界面

    产品特点列出cron职位修改cron作业删除cron作业创建Cron职位要求Python烧瓶Flask-JSGlue 所有使用的Crontab(由crontab.cfg中的前缀定义) 该程序必须以root身份运行安装pip install Flaskpip install Flask-...

    CronExpression(克龙表达式)的验证代码JavaScript

    4. `format`:类似于C#的`string.Format`,用于格式化字符串。 在验证Cron表达式时,可能需要利用这些工具函数来预处理输入,确保其格式正确。 总结来说,这篇博客文章可能会介绍如何在JavaScript环境中实现Cron...

    generate xml format file

    标题“generate xml format file”指的是创建XML格式的文件,这是一项常见的数据序列化任务,尤其在编程中。XML(eXtensible Markup Language)是一种结构化的数据存储方式,广泛用于数据交换、配置文件以及Web服务...

    crontab:ron Cron表达式生成器

    一个简单的Web界面,可轻松生成cron表达式。 贡献 随意建议功能或打开拉取请求。 在本地运行 $ git clone git@github.com:cronhub-app/crontab.git $ cd crontab $ yarn $ yarn start 在浏览器中查看运行的应用...

    cron_mdp:cron像MDP的作业调度程序(ZeroMQ Majordomo协议)

    5. **Message Format**:MDP定义了任务和响应的格式,使得不同组件间能无歧义地进行通信。在cron_mdp中,任务描述可能包括执行命令、参数、执行时间等信息。 6. **Fault Tolerance**:利用ZeroMQ的特性,cron_mdp...

    agqr-recorder:以cron格式录制超!A&G程序

    Agqr :: Recorder Agqr :: Recorder可以录制超!A&G的程序。 它以cron格式(使用 )指定日期和时间。 安装 将此行添加到您的应用程序的... schedule: cron_format length: recording_time 贡献 分叉( ) 创

    ansible-role-mysql-backup:为常规的完整MysqlMariadb数据库转储设置cron作业

    mysql_backup_filename_format :包含unix date格式序列的文件名,默认{{ ansible_hostname }}-%Y%m%d-%H%M%S.mysqldump 。 这可用于自动滚动覆盖备份。 mysql_backup_frequency :该名称必须与标准/etc/cron.*...

    Quantum-Core:Elixir的类似Cron的作业调度程序

    **量子核心(Quantum-Core):Elixir的Cron式任务调度器** 在Elixir编程语言中,开发人员经常需要安排定期的任务执行,这在Web应用程序和服务中是常见的需求,例如定时备份、发送电子邮件报告或者执行数据分析。...

    利用Crontab实现对Oracle数据库的定时备份.rar

    可以使用`journalctl -u cron`或者`grep CRON /var/log/syslog`来查看Cron的日志,检查是否有错误信息。 5. **备份策略优化**: - 根据业务需求,可以调整备份策略,比如设置增量备份、差异备份,或者备份特定的表...

    linux环境下oracle备份脚本参照.pdf

    此文档"linux环境下oracle备份脚本参照.pdf"提供了几个示例脚本,详细介绍了如何使用RMAN(恢复管理器)进行全量和增量备份,以及如何利用Cron定时任务自动化执行这些备份过程。以下是对这些知识点的详细说明: 1. ...

    RMAN备份技术解决方案.pdf

    备份控制文件通常有两种格式:格式化备份(format)和非格式化备份(nocompress)。格式化备份允许你指定备份文件的存储位置和名称,而非格式化备份则使用RMAN管理的默认命名规则。 `backup as compressed ...

    关于给springboot添加定时器的两种方式

    System.out.println("当前时间为: " + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } ``` 这里的Cron表达式`"0/2 * * * * ?"`表示每2秒执行一次该方法。Cron表达式是一个字符串...

    PHP 时间函数:与当前时间比….rar

    例如,`$datetime->format('Y-m-d H:i:s')`会将日期时间格式化为"年-月-日 时:分:秒"。 5. **比较时间**: 要比较两个时间点,你可以使用`DateTime::diff`方法。这将返回一个`DateInterval`对象,表示两个日期间的...

    详解Spring Boot中使用@Scheduled创建定时任务

    System.out.println("现在时间:" + sdf.format(new Date())); } } ``` 在上面的例子中,我们使用 @Scheduled(fixedRate = 5000) 注解来定义每 5 秒执行一次的任务。 @Scheduled 注解的使用 @Scheduled 注解...

    time.clj:Clojure的时间实用程序(脚本)

    3. **CRON 表达式支持**:`time.clj` 包含对 CRON 表达式的解析和执行支持,这使得在 Clojure 中设置基于 CRON 的定时任务成为可能。CRON 是一种广泛使用的定时任务调度格式,允许用户定义按特定时间间隔执行的任务...

Global site tag (gtag.js) - Google Analytics