[英]cron job to run each minutes from monday to friday from 9:15AM to 3:30PM
[英]Schedule cron job from 9:21AM till 02:30PM every 2 minutes
我想安排一个cron job
从周一到周五每 2 分钟从上午 9:21 到下午 02:30 运行。 我怎样才能做到这一点?。 我知道以下可以从上午 9 点到下午 4 点执行此操作,我该如何修改它以实现上述条件。
提前致谢。
*/2 09-16 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
这实际上是正确 cron 表达式的开始。 为了得到你正在寻找的东西,你实际上必须结合几个预定的表达式,我相信
我强烈推荐使用工具Crontab Guru来检查你的 crontab 表达式!
# “At every 2nd minute from 21 through 60 past hour 9 on every day-of-week from Monday through Friday.”
21-60/2 9 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
# At every 2nd minute past every hour from 10 AM through 1:59 PM on every day-of-week from Monday through Friday.
*/2 10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
# At every 2nd minute from 0 through 30 past hour 14 on every day-of-week from Monday through Friday.
0-30/2 14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
另一个可能有用的注意事项是检查 cron 邮件(我看到您正在记录,这很棒。)但系统通常也会提供详细说明 cron 作业的 cron 邮件。
通常可以在 /var/spool/mail/{username} 中找到
手册页 ( man 5 crontab
) 中没有指示任何单行规范都支持您需要的内容,因为您设置的任何范围都将分别应用于每个时间字段(例如分钟、小时),例如21-30/2 9-14...
意味着在每个小时之后的 21、23、25、27、29 分钟运行。
您当然可以使用多行来达到预期的效果:
21-59/2 9 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-59/2 10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-30/2 14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
在这种情况下,间隔是一个小时的因子这一事实有所帮助,因此至少这三行的中间将确保在 10:01 - 13:59 期间有规律的间隔。 如果您有 7 分钟的间隔,那么您将需要更多的行来确保始终有一个完全规则的间隔。
另请注意手册页中的以下注释:
crontab 语法无法定义所有可能的时期,人们可以想象出来。 例如,定义一个月的最后一个工作日并不简单。 如果任务需要在 crontab 语法中无法定义的特定时间段内运行,最好的方法是让程序本身检查日期和时间信息,并仅在时间段与所需时间段匹配时继续执行。
因此,您可以采用这种方法并仅使用例如:
1-59/2 9-14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
并在您的 shell 脚本(或者可能是包装器脚本)中执行一些测试,例如:
hhmm=`date +%H%M`
if [ $hhmm -lt 0930 -o $hhmm -gt 1430 ]; then exit; fi
(这里我们将hhmm
视为 4 位十进制数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.