📜  编写Cron表达式以安排任务

📅  最后修改于: 2021-08-25 18:40:48             🧑  作者: Mango

Cron是在类Unix操作系统上可用的广泛使用的软件实用程序,可用于作业调度。某些可能需要偶尔运行的程序或脚本被添加为Cron作业,并定义了时间表以描述何时运行此作业。 Cron表达式提供了一种指定此计划的方式。此外,Cron表达式广泛用于不同的应用程序和库中,以出于各种目的表达复杂的基于时间的时间表。

Cron Schedule示例:
Cron表达式旨在指定必须执行计划任务的日期和时间。使用Cron表达式,我们可以指定时间表,例如以下内容。

  • 每1小时每分钟运行一次。
  • 从每小时的15分钟标记开始,每小时运行一次。
  • 每小时运行一次,但凌晨02:00之间的时间除外。和凌晨05:00

上面的列表提供了可以使用单个cron表达式编写的时间表的非常基本的列表。
Cron表达式格式:
cron表达式是一个简单的字符串,由6到7个字段之间的任意位置组成,每个字段之间用空格隔开。下面指定了由7个字段组成的最常见的cron表达式,它们表示时间的各种面额。在这7个字段中,前6个字段是必填字段,而最后一个字段()是可选字段。

      

基于为以上每个组件指定的值,可以创建复杂的计划。
Cron表达式中使用的特殊字符:

Character Meaning Example
* All. Represents that the schedule should run for every time unit A “*” in the minute field indicates that the schedule runs every minute 
? Any. Represents any arbitrary value. This can be used only in day-of-month and day-of-week fields A “?” in day-of-month field will not use the day-of-month for deciding the schedule as any value is acceptable here
Range. Represents a continuous range of values. Using “5-8” in the field indicates the hours 5, 6, 7 and 8
, Multiple Values. Separates a list of different values Using “5, 6, 10” in the field indicates the hours 5, 6 and 10
/ Increment. Specifies the amount by which to increment the values of a field 3/5 in the minute field indicates the minutes 3, 8, 13, …, 58 in an hour. */10 in the minute field indicates the minutes 0, 10, 20…, 60

创建时间表:
对于上面指定的字段,可以使用值的组合来使用Cron表达式创建所需的计划。作为示例,让我们尝试了解如何构造一些示例cron表达式。

  1. 每天上午09:23:15运行一次–
    由于日程表每天,每个月和每年运行,因此这些字段将用*标记。对于星期几或每月几号,我们必须使用“?”其中一个表示“ *”,另一个表示“ *”(不能同时具有“?”或“ *”)。此外,小时字段将为9,分钟字段将为23,秒字段将为0。因此,表达式为0 9 23 * *? *。
  2. 从每天的06:00 pm开始到10:00.pm结束的每一分钟,每天–
    时间表必须在每分钟开始时运行,即每分钟第二个0。因此,第二个字段为0,分钟字段为*。时间范围是从06:00 pm到10:00:pm。这可以表示为18-22,因为小时是按0-23比例解析的。因此,表达式为:0 * 18-22 * *? *。
  3. 从每天的04:00 pm到每天的04:55 pm每隔5分钟运行一次–
    要每5分钟运行一次计划,在小时字段中使用的增量为5,如下所示0/5。对于小时字段,由于计划仅在04:00 pm的小时内运行,因此要使用的值仅为16。因此表达式为:0 0/5 16 * *?。

了解Cron表达式:
以下是cron表达式的进一步示例及其含义:

Expression Meaning
0 */5 * ? * * Once every five minutes
0 20, 30, 45 * ? * * Every hour at minutes 20, 30 and 45 of the hour (Thus thrice in each hour)
0 30, 45 14 ? 1-5 Monday At 2:30 p.m. and 2:45 p.m. every Monday in the months January to May (1-5)
0 0 9 ? * MON-FRI Every 09:00 a.m. from Monday to Friday
15 30 * ? * * At the 15th second of the 30th minute for every hour. E.g. 10:30:15, 11:30:15, …
25 30 10 * * ? 2021 At 10:30:25 a.m. every day in the year 2021
0 20 8 ? * 3L 2020-2022 At 08:20 a.m. on every last Tuesday of each month for the years 2020, 2021 and 2022

在Unix中使用带有命令的Cron表达式:
在Unix中,可以通过crontab(cron表)指定shell命令列表以及执行计划。 crontab条目的语法是计划表达式+要运行的shell命令。因此,格式如下。

       

例子 –
例如,以下crontab条目在每个星期六的22:30(晚上10:30)执行一个名为clear_logs.sh的shell程序。

0 30 22 ? * SAT /home/scripts/clear_logs.sh

有关使用Cron表达式的注意事项:

  • 不能在同一cron表达式中同时指定“月”和“周”字段。如果两个值之一由*表示,则另一个必须由?表示
  • 如果将计划时间设置为在12:00 am和1:00 am之间发生,与夏令时相关的更改可能会导致跳过或重复计划的点火。
  • 如果需要,该表达式可能已拆分为2。例如,要表示一个会在除凌晨03:00和06:00之间触发的每分钟触发的时间表,将需要以下两个表达式: 0 * 0-2 * *?在12:00 AM和03:59 AM之间每分钟运行一次计划,另一个表达式为0 * 6-23 * *?每分钟06:00 AM和11:59 PM之间运行。