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
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 %
) 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 theSHELL
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