The Difference Between Cron Syntax in Linux and Spring – Linux和Spring中Cron语法的区别

最后修改: 2020年 7月 25日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Cron expressions enable us to schedule tasks to run periodically at a specific date and time. After its introduction in Unix, other Unix-based operating systems and software libraries (including the Spring Framework) adopted its approach for task scheduling.

Cron表达式使我们能够安排任务,在特定的日期和时间定期运行。在Unix中引入后,其他基于Unix的操作系统和软件库(包括Spring框架)也采用了它的方法进行任务调度。

In this quick tutorial, we’re going to see what’s the difference between Cron expressions in Unix-based operating systems and the Spring Framework.

在这个快速教程中,我们将看到基于Unix的操作系统中的Cron表达式与Spring框架之间有什么区别。

2. Unix Cron

2.Unix Cron

Cron in most Unix-based systems has five fields: minute (0-59), hour (0-23), day of the month (1-31), months (1-12 or names), and day of the week (0-7 or names).

Cron在大多数基于Unix的系统中有五个字段。分钟(0-59)、小时(0-23)、月日(1-31)、月(1-12或名称)和星期(0-7或名称)。

We can put some special values in each field, like an asterisk (*):

我们可以在每个字段中放入一些特殊的值,如星号(*)。

5 0 * * *

The job will be executed 5 minutes after midnight every day. It’s also possible to use a range of values:

该工作将在每天午夜后5分钟执行。也可以使用一个值的范围。

5 0-5 * * *

Here the scheduler will execute the task 5 minutes after midnight, and also 5 minutes after 1, 2, 3, 4, and 5 o’clock every day.

在这里,调度器将在午夜后5分钟执行任务,在每天的1、2、3、4、5点后也执行5分钟。

Or, we can use a list of values:

或者,我们可以使用一个值的列表。

5 0,3 * * *

Now the scheduler executes the job five minutes after midnight and five minutes after 3 o’clock every day. The original Cron expression offers many more features than what we covered so far.

现在,调度器在每天午夜后5分钟和3点钟后5分钟执行工作。原始的Cron表达式提供了更多的功能,而不是我们到目前为止所涉及的。

However, it comes with one big limitation: We can’t schedule jobs with second precision since it doesn’t have a dedicated second field.

然而,它有一个很大的限制。由于它没有专门的第二字段,我们无法安排具有第二精度的工作。

Let’s see how Spring manages to fix this limitation.

让我们看看Spring是如何解决这个限制的。

3. Spring Cron

3.Spring里的克朗

To schedule periodic background tasks in Spring, we usually pass a Cron expression to the @Scheduled annotation.

要在Spring中安排定期的后台任务,我们通常会将一个Cron表达式传递给@Scheduled注释。

As opposed to Cron expressions in Unix-based systems, the Cron expression in Spring has six space-separated fields: second, minute, hour, day, month, and weekday.

与基于Unix的系统中的Cron表达式不同,Spring中的Cron表达式有六个空格分隔的字段:秒、分、小时、日、月、星期

For instance, to run a task every ten seconds we can do:

例如,为了每十秒钟运行一个任务,我们可以这样做。

*/10 * * * * *

Also, to run a task every 20 seconds from 8 am to 10m every day:

另外,每天从早上8点到晚上10点,每20秒运行一个任务。

*/20 * 8-10 * * *

As shown in the above examples, the first field represents the second portion of the expression. That’s the difference between the two implementations. Despite the difference in the second field, Spring supports many features from the original Cron such as range numbers or lists.

如上面的例子所示,第一个字段代表表达式的第二部分。这就是两种实现之间的区别。尽管第二个字段有区别,但Spring支持原始Cron的许多特性,如范围数字或列表。

From the implementation’s perspective, the CronSequenceGenerator class is responsible for parsing the Cron expressions in Spring.

从实现的角度来看,CronSequenceGenerator类负责解析Spring中的Cron表达式。

4. Conclusion

4.总结

In this short tutorial, we saw the Cron implementation difference between Spring and most Unix-based systems. Along the way, we saw a few examples of both implementations.

在这个简短的教程中,我们看到了Spring和大多数基于Unix的系统之间的Cron实现差异。一路走来,我们看到了两种实现方式的一些例子。

In order to see more examples of Cron expressions, it’s highly recommended to check out our Guide to Cron Expressions. Moreover, taking a look at the source code of the CronSequenceGenerator class can give us a good idea of how Spring implements this feature.

为了看到更多的Cron表达式的例子,强烈建议你查看我们的Guide to Cron Expressions。此外,看看CronSequenceGenerator类的源代码可以让我们对Spring如何实现这一功能有一个很好的了解。