问题描述
我的任务是在考虑 YEAR 字段的 CRON 中指定时间.我该怎么做,或者你知道任何可以在我的 linux 服务器上帮助我的东西吗?谢谢
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
推荐答案
如之前的帖子所述,您不能指定年份字段,但是,可以模仿它:
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
这里,command1
将在 2020-01-01T00:00:00 上运行,command2
将在每 3 年的一月一日午夜运行一次,它将在 2019、2022、2025 上运行,......command3
与 command2
的作用相同,但有一年的偏移量,即 2020, 2023, 2026, ...
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, ...
注意:不要忘记您必须在 crontab 文件中转义 <percent> 字符 (%
):
note: don't forget that you have to escape the <percent>-character (%
) in your crontab file:
第六个"字段(行的其余部分)指定要运行的命令.该行的整个命令部分,直到换行符或"%
" 字符,将由 /bin/sh
或 cronfile 的 SHELL
变量中指定的 shell 执行.命令中的%
"字符,除非用反斜杠()转义,否则会被改成换行符,第一个
% 将作为标准输入发送到命令.
这篇关于考虑到 YEAR,如何在 CRON 中指定时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!