Run crontab in specific day but only if is in specific weekday
Solution 1:
I don't think it is possible with cron
alone because the syntax doesn't
allow for "at 27th except on Sundays". It only allows
for "either 27th or non-Sundays (or both)".
I suggest to either change command.sh
to immediately exit on non-Sundays
or put that check into the cronjob:
1 0 27 12 * test $(date +\%u) -ne 7 && command.sh
At the shell, date +%u
returns the day of week (1…7, 1 is Monday). In
a cronjob we have to escape that %
sign (\%
). The command will
check whether the current day is a non-Sunday and only then execute
command.sh
.
The cronjob will run on every Dec 27th at 00:01 o'clock,
no matter what day of the week that is, but only for non-Sundays the
command.sh
gets executed because the previous test only succeeds
for them.
Note: I sometimes use https://crontab.guru to check my cronjob's
timetables. It's quite handy.
Your definition translates to “At
00:01 on day-of-month 27 and on every day-of-week from Monday through
Saturday in December.” so it's quite the opposite of what you want.
From the manpage:
Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), 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. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).…
# Run on every second Saturday of the month 0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"