I have to schedule the cron to run at specified time. I have the command like below. But I could not understand how to run only once in a year at a specified date and time.
My question is: what should the value for 5th field (Day of the Week) be?
30 06 02 01 * bash -x /home/user/cron-specified-time.sh
Time/date: Jan 2nd 06:30 AM – only once in a year
Advertisement
Answer
Quote from the crontab(5) man page:
The day of a command’s execution can be specified in the following two fields — ‘day of month’, and ‘day of week’. If both fields are restricted (i.e., do not contain the
*
character), the command will be run when either field matches the current time. For example,30 4 1,15 * 5
would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
So, in your case, as you’ve set day of month already, you can just leave the day of the week as *
.
Alternatively, from the POSIX spec (emphasis mine):
The specification of days can be made by two fields (day of the month and day of the week).
If month, day of month, and day of week are all <asterisk> characters, every day shall be matched.
If either the month or day of month is specified as an element or list, but the day of week is an <asterisk>, the month and day of month fields shall specify the days that match.
If both month and day of month are specified as an <asterisk>, but day of week is an element or list, then only the specified days of the week match.
Finally, if either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the day of week, shall be matched.