我。将e xtend Keith Thompson回答:
他的解决方案每隔5分钟完美工作一次,但不会工作,比如说,每13分钟一次;如果我们使用$minutes % 13
我们得到这个时间表:
5:13
5:26
5:30
5:52
6:00 because 0%13 is 0
6:13
...
我相信你会注意到这个问题。我们可以实现任意频率,如果再算上分钟(,小时,天或数周),因为Epoch:
#!/bin/bash
minutesSinceEpoch=$(($(date +'%s/60')))
if [[ $(($minutesSinceEpoch % 13)) -eq 0 ]]; then
php [...]
fi
date(1)
返回当前的日期,我们因为大纪元格式设置为秒(%s
),然后我们做基本的数学:
# .---------------------- bash command substitution
# |.--------------------- bash arithmetic expansion
# || .------------------- bash command substitution
# || | .---------------- date command
# || | | .------------ FORMAT argument
# || | | | .----- formula to calculate minutes/hours/days/etc is included into the format string passed to date command
# || | | | |
# ** * * * *
$(($(date +'%s/60')))
# * * ---------------
# | | |
# | | ·----------- date should result in something like "1438390397/60"
# | ·-------------------- it gets evaluated as an expression. (the maths)
# ·---------------------- and we can store it
而且你可以使用在OpenShift与每小时,每天或每月cron作业这种方法:
#!/bin/bash
# We can get the
minutes=$(($(date +'%s/60')))
hours=$(($(date +'%s/60/60')))
days=$(($(date +'%s/60/60/24')))
weeks=$(($(date +'%s/60/60/24/7')))
# or even
moons=$(($(date +'%s/60/60/24/656')))
# passed since Epoch and define a frequency
# let's say, every 7 hours
if [[ $(($hours % 7)) -ne 0 ]]; then
exit 0
fi
# and your actual script starts here
的NotI我使用了-ne
(不等于)运算符来退出脚本,而不是使用-eq
(等于)运算符将脚本包装到IF构造中;我发现它很方便。
并记住为您的频率使用正确的.openshift/cron/{minutely,hourly,daily,weekly,monthly}/
文件夹。
以上的答案是错误的,从执行50-59分钟,每分钟。所以这个改变会改变条件为“if [$(($ minute%5))-eq 0]; then”并且它有效。由于某种原因,我的编辑被拒绝了。所以,我只是在评论中留下正确的答案。作者或其他人随时可以用本声明编辑答案。 – theshadowmonkey
@theshadowmonkey:完成。感谢您的支持。我已经包括了几个选择(这可能是矫枉过正)。 –
@theshadowmonkey使用if [$(($ minute%5))-eq 0];那么我得到的错误:08:价值太大基地(错误标记是“08”) –