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

发布于 2024-12-09 11:29:24 字数 1827 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁憨憨 2024-12-16 11:29:24

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

# 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

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

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

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

来源:man 5 crontab

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, command1 will run on the 2020-01-01T00:00:00, command2 will run every 3 years on the first of January at midnight, it will run so on 2019, 2022, 2025, ... . command3 does the same as command2 but has one year offset, i.e. 2020, 2023, 2026, ...

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

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/sh or by the shell specified in the SHELL variable 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

星星的軌跡 2024-12-16 11:29:24

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

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.

缺⑴份安定 2024-12-16 11:29:24

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

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

入怼 2024-12-16 11:29:24
var task = cron.schedule('0 0 1 1 *', () => {
    console.log('Printing this line 1ST JANUARY OF EVERY YEAR in the terminal');
});

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文