简体   繁体   中英

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. 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.

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. 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!

# “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.

Typically this is found in /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.

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. If you had an interval of, say, 7 minutes, then you would need even more lines to ensure a completely regular interval throughout.

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. 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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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