我的任务是在CRON中指定YEAR字段的时间.我怎么能这样做,或者你知道哪些东西可以帮助我在linux服务器上?谢谢
kva*_*our 12
正如之前的帖子所指出的,您不能指定年份字段,但是,可以模仿它:
# 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
Run Code Online (Sandbox Code Playgroud)
在这里,command1将在 2020-01-01T00:00:00 上command2运行,将在 1 月 1 日午夜每 3 年运行一次,它将在 2019、2022、2025上运行,......。command3与相同command2但有一年的抵消,即 2020, 2023, 2026, ...
注意:不要忘记您必须%在 crontab 文件中转义 <percent> 字符 ( ):
“第六个”字段(行的其余部分)指定要运行的命令。该行的整个命令部分,直到换行符或 "
%" 字符,将由cronfile/bin/sh的SHELL变量中指定的 shell执行或由 shell执行。%命令中的" " 字符,除非用反斜杠 (\)转义,否则将更改为换行符,并且第一个之后的所有数据%将作为标准输入发送到命令。来源:
man 5 crontab
Crontab(5)文件格式没有YEAR字段。您可以尝试@yearly(在元旦的00:00)运行cron作业,该作业使用date(1)查看当年,并将当前的crontab文件更新为适合该新年的文件。