crontable OR 條件

crontable OR 條件

Crontable 參數似乎起到「與」條件的作用。所以這個例子

0 9-5 * * 1-5

當滿足條件「分鐘為零且小時在 9 到 5 之間且日期在星期一和星期五之間」時運行。

我想要的是一個“或”函數,所以我可以說“週一到週五或該月的第 8 天運行”。這樣的事情存在嗎?

我意識到您可以添加兩個條目,但條目很多,它會增加一些容易忘記的內容。

答案1

我想說:使用,(逗號)。從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 日以及週一到週五的每一天運行您的命令。

另一個解是使用test( man bash、條件表達式部分和man date):

# Run on every second Saturday of the month:
   0 4 8-14 * *    test $(date +%u) -eq 6 && echo "2nd Saturday"

相關內容