1. Overview
1.概述
Simply put, cron is a basic utility available on Unix-based systems. It enables users to schedule tasks to run periodically at a specified date/time. And it’s naturally a great tool for automating lots of process runs, which otherwise would require human intervention.
简单地说,cron是一个基于Unix系统的基本工具。它使用户能够安排任务,在指定的日期/时间定期运行。它自然是一个自动运行大量进程的伟大工具,否则就需要人工干预。
Cron runs as a daemon process. This means it only needs to be started once and it will keep running in the background. This process makes use of crontab to read the entries of the schedules and kicks off the tasks.
Cron作为一个守护进程运行。这意味着它只需要启动一次,而且会在后台持续运行。这个进程利用crontab来读取时间表的条目,并启动任务。
Over time, the cron expression format became widely adopted, and many other programs and libraries make use of it.
随着时间的推移, cron表达式格式被广泛采用,许多其他程序和库都在使用它。
2. Working With Crontab
2.使用Crontab工作
A cron schedule is a simple text file located under /var/spool/cron/crontabs on Linux systems. We cannot edit the crontab files directly, so we need to access it using the crontab command.
cron时间表是一个简单的文本文件,在Linux系统上位于/var/spool/cron/crontabs下。我们不能直接编辑crontab文件,所以我们需要使用crontab命令访问它。
To open crontab file, we need to fire this command:
要打开crontab文件,我们需要启动这个命令。
crontab -e
Each line in crontab is an entry with an expression and a command to run:
crontab中的每一行都是一个条目,包含一个表达式和一个要运行的命令。
* * * * * /usr/local/ispconfig/server/server.sh
This entry runs the mentioned script every single minute.
这个条目每隔一分钟就会运行上述脚本。
3. Cron Expression
3.Cron的表达方式
Let’s understand the cron expression.
让我们了解一下cron表达式。
It consists of five fields:
它由五个领域组成。
<minute> <hour> <day-of-month> <month> <day-of-week> <command>
3.1. Special Characters in Expression
3.1.表达式中的特殊字符
- * (all) specifies that event should happen for every time unit. For example, “*” in the <minute> field means “for every minute.”
- ? (any) is utilized in the <day-of-month> and <day-of -week> fields to denote the arbitrary value and thus neglect the field value. For example, if we want to fire a script at “5th of every month” irrespective of what day of the week falls on that date, we specify a “?” in the <day-of-week> field.
- – (range) determines the value range. For example, “10-11” in the <hour> field means “10th and 11th hours.”
- , (values) specifies multiple values. For example, “MON, WED, FRI“ in <day-of-week> field means on the days “Monday, Wednesday and Friday.”
- / (increments) specifies the incremental values. For example, a “5/15” in the <minute> field means at “5, 20, 35 and 50 minutes of an hour.”
- L (last) has different meanings when used in various fields. For example, if it’s applied in the <day-of-month> field, it means last day of the month, i.e. “31st of January” and so on as per the calendar month. It can be used with an offset value, like “L-3”, which denotes the “third to last day of the calendar month.” In <day-of-week>, it specifies the “last day of a week.” It can also be used with another value in <day-of-week>, like “6L”, which denotes the “last Friday.”
- W (weekday) determines the weekday (Monday to Friday) nearest to a given day of the month. For example, if we specify “10W” in the <day-of-month> field, it means the “weekday near to 10th of that month.” So if “10th” is a Saturday, the job will be triggered on “9th,” and if “10th” is a Sunday, it will trigger on “11th.” If we specify “1W” in <day-of-month> and if “1st” is Saturday, the job will be triggered on “3rd,” which is Monday, and it will not jump back to the previous month.
- # specifies the “N-th” occurrence of a weekday of the month, for example, “third Friday of the month” can be indicated as “6#3”.
3.2. Cron Expression Examples
3.2.Cron表达式示例
Let’s see some examples of cron expressions by using the fields and special characters combinations:
让我们看看通过使用字段和特殊字符组合的cron表达式的一些例子。
At 12:00 p.m. (noon) every day:
每天下午12点(正午)。
0 12 * * ?
Every five minutes starting at 1 p.m. and ending at 1:55 p.m. and then starting at 6 p.m. and ending at 6:55 p.m., every day:
从下午1点开始每5分钟一次,到下午1点55分结束,然后从下午6点开始,到下午6点55分结束,每天如此。
0/5 13,18 * * ?
Every minute starting at 1 p.m. and ending at 1:05 p.m., every day:
每天从下午1点开始,到下午1点05分结束的每一分钟。
0-5 13 * * ?
At 1:15 p.m. and 1:45 p.m. every Tuesday in the month of June:
6月份的每个星期二下午1:15和1:45。
15,45 13 ? 6 Tue
At 9:30 a.m. every Monday, Tuesday, Wednesday, Thursday and Friday:
每周一、二、三、四和五上午9:30。
30 9 ? * MON-FRI
At 9:30 a.m. on the 15th day of every month:
每月15日上午9:30。
30 9 15 * ?
At 6 p.m. on the last day of every month:
每个月的最后一天下午6点。
0 18 L * ?
At 6 p.m. on the third to last day of every month:
每个月的第三天到最后一天的下午6点。
0 18 L-3 * ?
At 10:30 a.m. on the last Thursday of every month:
每个月的最后一个星期四上午10:30。
30 10 ? * 5L
At 10 a.m. on the third Monday of every month:
每个月的第三个星期一上午10点。
0 10 ? * 2#3
At 12 midnight on every day for five days starting on the 10th day of the month:
从每月10日开始的五天里,每天午夜12点。
0 0 10/5 * ?
4. Cron Special Strings
4.Cron特殊弦乐
In addition to the fields specified in the cron expression, there’s also support for some special, predefined values that we can use instead of the fields:
除了在cron表达式中指定的字段之外,还支持一些特殊的、预定义的值,我们可以用这些值来代替字段。
- @reboot – run once at the startup
- @yearly or @annualy – run once a year
- @monthly – run once a month
- @weekly – run once a week
- @daily or @midnight – run once a day
- @hourly – run hourly
5. Conclusion
5.结论
In this quick article, we’ve explored cron jobs and crontab.
在这篇快速文章中,我们已经探讨了cron作业和crontab。
We’ve also seen a number of expression examples we can use in our daily work or simply infer other expressions from.
我们还看到了一些表达式的例子,我们可以在日常工作中使用,或者简单地推断出其他表达式。