I wanna run command with crontab in a specific day, but only if this day is not sunday. I tried with this
1 0 27 12 1-6 command.sh
but the command run also wednesday december 26th. How can I tell to crontab run command only the december 27th?
Thanks Max
31 Answer
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.shAt 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 executecommand.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 thecommand.sh gets executed because the previous test only succeeds
for them.
Note: I sometimes use 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:
2Note: 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 * 5would 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"