简体   繁体   English

从上午 9 点 21 分到下午 2 点 30 分每 2 分钟安排一次 cron 作业

[英]Schedule cron job from 9:21AM till 02:30PM every 2 minutes

I want to schedule a cron job to run from 9:21 AM till 02:30 PM every 2 minutes from Monday to Friday.我想安排一个cron job从周一到周五每 2 分钟从上午 9:21 到下午 02:30 运行。 How can I do this?.我怎样才能做到这一点?。 I know the following can do this from 9 AM till 4 PM, how can I modify this to achieve the above condition.我知道以下可以从上午 9 点到下午 4 点执行此操作,我该如何修改它以实现上述条件。

Thanks in advance.提前致谢。

*/2 09-16 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

This is actually the start of the right cron expression.这实际上是正确 cron 表达式的开始。 To get what you're looking for, you'll actually have to combine several scheduled expressions, I believe为了得到你正在寻找的东西,你实际上必须结合几个预定的表达式,我相信

I highly recommend using the tool Crontab Guru to check your crontab expressions!我强烈推荐使用工具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



Another note that could be helpful is checking the cron mail (I see you're logging, which is great.) but the system usually delivers cron mail detailing cron jobs as well.另一个可能有用的注意事项是检查 cron 邮件(我看到您正在记录,这很棒。)但系统通常也会提供详细说明 cron 作业的 cron 邮件。

Typically this is found in /var/spool/mail/{username}通常可以在 /var/spool/mail/{username} 中找到

There is no indication in the man page ( man 5 crontab ) that what you require is supported in any single line specification, as any ranges that you set will be applied for each time field (eg minute, hour) separately, so for example 21-30/2 9-14... would mean to run at 21,23,25,27,29 minutes past each of those hours.手册页 ( man 5 crontab ) 中没有指示任何单行规范都支持您需要的内容,因为您设置的任何范围都将分别应用于每个时间字段(例如分钟、小时),例如21-30/2 9-14...意味着在每个小时之后的 21、23、25、27、29 分钟运行。

You can of course achieve the desired effect using multiple lines:您当然可以使用多行来达到预期的效果:

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

In this case, it is helped a little by the fact that the interval is a factor of an hour, so at least the middle of these three lines will ensure regular intervals during the period 10:01 - 13:59.在这种情况下,间隔是一个小时的因子这一事实有所帮助,因此至少这三行的中间将确保在 10:01 - 13:59 期间有规律的间隔。 If you had an interval of, say, 7 minutes, then you would need even more lines to ensure a completely regular interval throughout.如果您有 7 分钟的间隔,那么您将需要更多的行来确保始终有一个完全规则的间隔。

Note also the following comment in the manual page:另请注意手册页中的以下注释:

The crontab syntax does not make it possible to define all possible periods one could image off. crontab 语法无法定义所有可能的时期,人们可以想象出来。 For example, it is not straightforward to define the last weekday of a month.例如,定义一个月的最后一个工作日并不简单。 If a task needs to be run in a specific period of time that cannot be defined in the crontab syntaxs the best approach would be to have the program itself check the date and time information and continue execution only if the period matches the desired one.如果任务需要在 crontab 语法中无法定义的特定时间段内运行,最好的方法是让程序本身检查日期和时间信息,并仅在时间段与所需时间段匹配时继续执行。

So you could adopt this approach and just use for example:因此,您可以采用这种方法并仅使用例如:

1-59/2 9-14     * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log

and perform some test in your shell script (or perhaps a wrapper script) such as:并在您的 shell 脚本(或者可能是包装器脚本)中执行一些测试,例如:

hhmm=`date +%H%M`
if [ $hhmm -lt 0930 -o $hhmm -gt 1430 ]; then exit; fi

(here we are treating hhmm as a 4-digit decimal number) (这里我们将hhmm视为 4 位十进制数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM