我要在每个月的第一个星期五晚上运行cronjob我使用了下面提到的条目
00 20 1-7 * Fri [ "$(date '+%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1
这个条目应该运行我的脚本,如果星期五落在1-7天的一个月,但我的脚本得到执行,甚至在第7(即在所有的星期五的一个月)。请建议如何修复
这是因为当您指定月中的一天和星期中的一天时,cron将在这两个约束中的任何一个为真时执行作业。从crontab(5)的手册页:
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 cur‐
rent 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.
在cron中没有直接的方法来做你想做的事情,但是cron:如何计划运行每个月的第一个星期天描述了一个解决方法,通过使用cron运行你的脚本,例如每个星期五,然后在脚本中计算每月的一天是否在1-7的范围内,只有在这种情况下才继续。
对于使用5而不是星期五来指定星期几的评论:使用星期五是可以的,正如手册页所说:
Months or days of the week can be specified by name.