Linux 考虑到 YEAR,如何在 CRON 中指定时间?

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

How can I specify time in CRON considering YEAR?

linuxcron

提问by mart7ini

My task is to specify time in CRON considering YEAR field. How can i do it, or do u know any stuff which can help me on my linux server? thx

我的任务是在考虑 YEAR 字段的 CRON 中指定时间。我该怎么做,或者你知道任何可以在我的 linux 服务器上帮助我的东西吗?谢谢

回答by Max

Crontab (5) file format has no YEAR field. You could try running a cron job @yearly (at 00:00 on New Year's day) which looks at the current year using date(1) and updates the current crontab file to one appropriate for the new year.

Crontab (5) 文件格式没有 YEAR 字段。您可以尝试运行一个 cron 作业 @yearly(在元旦的 00:00),它使用 date(1) 查看当前年份并将当前的 crontab 文件更新为适合新年的文件。

回答by nullability

Standard crondoesn't support a year field, but it's worth noting that some implementations support an extended crontab format with a sixth yearfield, such as nnCron. Not every cron is created equal.

标准cron不支持年份字段,但值得注意的是,某些实现支持具有第六个year字段的扩展 crontab 格式,例如nnCron。并非每个 cron 生来平等。

回答by Dhruv Parmar

var task = cron.schedule('0 0 1 1 *', () => {
    console.log('Printing this line 1ST JANUARY OF EVERY YEAR in the terminal');
});

It is work for considering YEAR field.. @mart7ini

这是考虑 YEAR 字段的工作.. @mart7ini

回答by kvantour

As indicated in earlier posts, you cannot indicate a year field, it is, however, possible to mimic it:

正如之前的帖子所指出的,您不能指定年份字段,但是,可以模仿它:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  0  0  1  1  *   [[ $(date "+\%Y") == 2020 ]] && command1
  0  0  1  1  *   (( $(date "+\%Y") % 3 == 0  )) && command2
  0  0  1  1  *   (( $(date "+\%Y") % 3 == 1  )) && command3

Here, command1will run on the 2020-01-01T00:00:00, command2will run every 3 years on the first of January at midnight, it will run so on 2019, 2022, 2025, ... . command3does the same as command2but has one year offset, i.e. 2020, 2023, 2026, ...

在这里,command1将在 2020-01-01T00:00:00 上command2运行,将在 1 月 1 日午夜每 3 年运行一次,它将在 2019、2022、2025上运行,......。command3与相同command2但有一年的抵消,即 2020, 2023, 2026, ...

note:don't forget that you have to escape the <percent>-character (%) in your crontab file:

注意:不要忘记您必须%在 crontab 文件中转义 <percent> 字符 ( ):

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or a "%" character, will be executed by /bin/shor by the shell specified in the SHELLvariable of the cronfile. A "%" character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first %will be sent to the command as standard input.

source: man 5 crontab

“第六个”字段(行的其余部分)指定要运行的命令。该行的整个命令部分,直到换行符或 " %" 字符,将由cronfile/bin/shSHELL变量中指定的 shell执行或由 shell执行。%命令中的" " 字符,除非用反斜杠 ( \)转义,否则将更改为换行符,并且第一个之后的所有数据%将作为标准输入发送到命令。

来源: man 5 crontab

回答by spaceman117X

There is actually one nice trick on how you specify the year on the cronjob for up to 6 years upfront by specifying the last star in the cronjob definition. We will take the following example:

实际上,通过指定 cronjob 定义中的最后一个星号,您可以提前 6 年指定 cronjob 上的年份,这是一个很好的技巧。我们将采用以下示例:

enter image description here

在此处输入图片说明

Here we see that in the year of 2020, the day 21st of May is set to be executed on Tuesday. Since the in the year 2020 the day 21st of May is Thursday, the cronjob will not execute the command.

在这里我们看到,在 2020 年,5 月 21 日将在星期二执行。由于 2020 年的 5 月 21 日是星期四,因此 cronjob 将不会执行该命令。

However, this command is going to be executed in year the 2024, 2030, 2036 and so on.

但是,该命令将在 2024、2030、2036 年等年份执行。