20

My task is to specify time in CRON considering YEAR field. How can I do it?

4
  • 3
    Workaround: run every year and move the year logic to the script. Personally, I wouldn't really trust a scheduled task that runs less than once a year. Too many things can change until you notice it skipped a run. Commented Oct 11, 2011 at 10:33
  • 3
    There is another usecase for using YEAR: If you only want execute a Job once. Commented Jul 15, 2019 at 15:59
  • @CodeFreezr, if you want to run a job a single time, you should use at instead of a crontab. at will execute your task a single time, similar as cron does. Commented Jan 29, 2021 at 7:54
  • What do you mean by "considering" (the) year field? Commented Sep 24, 2024 at 17:49

4 Answers 4

17

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

Sign up to request clarification or add additional context in comments.

2 Comments

Might be worth mentioning that I had to put a backslash to make the date evaluation work (in crontab -e; while it worked good directly in bash): [[ $(date "+\%Y").
@Kamafeather You are correct, you have to escape the % in cron.
8

Crontab (5) file format has no YEAR field. You could try running a cron job @yearly (at 00:00 on New Year's day) which looks at the current year using date(1) and updates the current crontab file to one appropriate for the new year.

1 Comment

but only feature is in needed for easily rewrite back and forward year field value unfortunately cumbrous logic
8

Standard cron doesn't support a year field, but it's worth noting that some implementations support an extended crontab format with a sixth year field, such as nnCron. Not every cron is created equal.

Comments

-2
var task = cron.schedule('0 0 1 1 *', () => {
    console.log('Printing this line 1ST JANUARY OF EVERY YEAR in the terminal');
});

It is work for considering YEAR field.. @mart7ini

1 Comment

If it runs every year, how does this consider the year?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.