Crontab 每月的第一周、第三周的星期三某个点执行一次

问题

  • 要求每个月的第一周第三周的星期三6点执行一次,开始以为挺简单立马想到
0 6 1-7,15-21 * 3  
  • 但是测试发现 1-7, 15-21 和 星期三也都会跑.

man 5 crontab 找到下述解释

    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.
    注: weekday 和 day 这两栏很容易造成混淆, 假如两栏同时都被指定时, 只需满足其中一栏就算符合.

可以依靠crontab+shell解决

0 6 1-7,15-21 * * if [ `date '+%w'` = "3" ]; then echo "exec cmd";fi