crontable OR条件


3

Crontab参数似乎充​​当'和'条件。所以这个例子

0 9-5 * * 1-5

满足条件时运行“分钟为零且小时在9到5之间,日期在星期一和星期五之间”。

我想要的是'或'功能,所以我可以说“星期一到星期五或每月的第8天”。这样的事情存在吗?

我意识到你可以添加两个条目,但是有很多条目会增加一些东西。

Answers:


3

我会说:用 , (逗号)。从 man 5 crontab

Lists are allowed.
A list is a set of numbers  (or  ranges)  separated  by  commas.
Examples:  "1,2,5,9","0-4,8-12".

但是你的情况稍微复杂一点,你可以利用这个功能:

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.

所以在你的情况下,你可以写 0 9-5 8 * 1-5。这将在每个月的8号和周一到周五的每一天执行命令。

另一种解决方案是使用a testman bash,部分条件表达,和 man date ):

# Run on every second Saturday of the month:
   0 4 8-14 * *    test $(date +%u) -eq 6 && echo "2nd Saturday"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.