Linux Crontab 星期几语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18919151/
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-08-07 00:54:01  来源:igfitidea点击:

Crontab Day of the Week syntax

linuxcroncrontabsysadmin

提问by Marty Wallace

In crontab does the Day of the Week field run from 0 - 6or 1 -7?

在 crontab 中,星期几字段从0 - 6或 开始运行1 -7

I am seeing conflicting information on this. wikipedia states 0-6and other sites I have seen are 1-7.

我看到有关此的相互矛盾的信息。维基百科状态0-6和我见过的其他网站是1-7.

Also what would be the implication or either using 0or 7incorrectly? i.e. would the cron still run?

还有什么含义或使用07不正确?即 cron 还会运行吗?

采纳答案by fedorqui 'SO stop harming'

0and 7both stand for Sunday, you can use the one you want, so writing 0-6 or 1-7 has the same result.

0并且7都代表星期天,你可以用你想要的那个,所以写 0-6 或 1-7 的结果是一样的。

Also, as suggested by @Henrik, it is possible to replace numbers by shortened name of days, such as MON, THU, etc:

此外,通过@Henrik的建议,也可以按天数,如缩短名称来代替数字MONTHU等:

0 - Sun      Sunday
1 - Mon      Monday
2 - Tue      Tuesday
3 - Wed      Wednesday
4 - Thu      Thursday
5 - Fri      Friday
6 - Sat      Saturday
7 - Sun      Sunday

Graphically:

图形化:

 ┌────────── minute (0 - 59)
 │ ┌──────── hour (0 - 23)
 │ │ ┌────── day of month (1 - 31)
 │ │ │ ┌──── month (1 - 12)
 │ │ │ │ ┌── day of week (0 - 6 => Sunday - Saturday, or
 │ │ │ │ │                1 - 7 => Monday - Sunday)
 ↓ ↓ ↓ ↓ ↓
 * * * * * command to be executed

Finally, if you want to specify day by day, you can separate days with commas, for example SUN,MON,THUwill exectute the command only on sundays, mondays on thursdays.

最后,如果你想逐天指定,你可以用逗号分隔几天,例如SUN,MON,THU只在星期日执行命令,在星期四星期一执行命令。

You can read further details in Wikipedia's article about Cron.

您可以在维基百科关于 Cron 的文章中阅读更多详细信息。

回答by Cyril Bouthors

You can also use day names like Monfor Monday, Tuefor Tuesday, etc. It's more human friendly.

您还可以使用Mon星期一、Tue星期二等日期名称。它更人性化。

回答by Henrik

    :-) Sunday    |    0  ->  Sun
                  |  
        Monday    |    1  ->  Mon
       Tuesday    |    2  ->  Tue
     Wednesday    |    3  ->  Wed
      Thursday    |    4  ->  Thu
        Friday    |    5  ->  Fri
      Saturday    |    6  ->  Sat
                  |  
    :-) Sunday    |    7  ->  Sun

As you can see above, and as said before, the numbers 0and 7are both assigned to Sunday. There are also the English abbreviated days of the week listed, which can also be used in the crontab.

正如您在上面看到的,如前所述,数字07都分配给星期日。还有列出的英文缩写的星期几,也可以在crontab中使用。

Examples of Number or Abbreviation Use

数字或缩写使用示例

15 09 * * 5,6,0             command
15 09 * * 5,6,7             command
15 09 * * 5-7               command
15 09 * * Fri,Sat,Sun       command

The four examples do all the same and execute a command every Friday, Saturday, and Sunday at 9.15 o'clock.

四个示例都做同样的事情,每周五、周六和周日 9.15 点执行一条命令。

In Detail

详细

Having two numbers 0and 7for Sunday can be useful for writing weekday ranges starting with 0or ending with 7. So you can write ranges starting with Sunday or ending with it, like 0-2or 5-7for example (ranges must start with the lower number and must end with the higher). The abbreviations cannot be used to define a weekday range.

有两个数字07for Sunday 可用于编写以 开头0或结尾的工作日范围7。所以,你可以写的范围开始星期日或与它的结局,像0-25-7例如(范围必须先从较低的数字,必须与更高端的)。缩写不能用于定义工作日范围。