How can I specify time in CRON considering YEAR?

前端 未结 4 1612
长情又很酷
长情又很酷 2021-01-02 01:55

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

4条回答
  •  一整个雨季
    2021-01-02 02:32

    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 -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

提交回复
热议问题