问题
I made a cron to execute a task every tuesday at 3:50 AM - except a tuesday which coincides with the first day of the month:
50 3 * * 2 MyCommand
but I don't know how I can translate my exception into the cron syntax, any tips?
回答1:
Can you put a conditional in your script? I would do that. And in your cron you can comment that it won't run on the first per your script directions
As an example, in bash you can do this:
#!/bin/bash
dayofmonth=`date +"%d"`
if [ $dayofmonth == "01" ];
then
# do not run, exit
exit
fi
# otherwise go on
echo "it is not the first"
So your cron would be
30 5 * * 2 /path/to/script # comment: script conditional in place to not run on the 1st
回答2:
You can set the "day of month" field to the range 2-31, effectively excluding the first day. This should do it:
50 3 2-31 * 2 MyCommand
来源:https://stackoverflow.com/questions/24508099/special-cron-expression-how-to-make-an-exception