gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:26:25 25 4
gpt4 key购买 nike

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

最佳答案

如前文所述,您不能指定年份字段,但是可以模仿它:

# 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 文件中转义 字符 ( %):

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

关于linux - 考虑到 YEAR,我如何在 CRON 中指定时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com