Say I have a crontab which runs every 20
minutes and I have a hour range which can vary so lets say a-b
, which in one example could look like
*/20 5-23 * * * /usr/bin/cool_program
My question is, will the cron run at 23:00, 23:20, 23:40 and 00:00 too?
For completeness, jobs in e.g. /etc/cron. {hourly,daily,weekly,monthly} are run sequentially. The run-parts script loops over all files in this directory. So you can use that in combination with a naming convention (similar to the numbering in /etc/init.
*/30 * * * * your_command. this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc) example #3. 0,30 * * * * your_command. this means "run when the minute of each hour is 0 or 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)
Yes, cronjobs can run at the same time, and will do so if you set them up that way. A 1 minute gap between each of the jobs might work, but what if one of them takes longer than a minute to run?
GK27's answer does not fully answer the question, so let me clarify:
cron
will run jobs when the time matches the expression provided. Your expression tells it to run when the minute is divisible by 20 (*/20
) and your hour range tells it to run when the hour is within the specified range inclusively (5-23
). The remaining three *
tell it to match any day, month, and any day of the week.
Therefore the first job will run at 05:00 because the hour, 05
, is in the range 5 to 23 and the minute, 00
, is divisible by 20. The last job will run at 23:40 because the hour, 23
, is in the range 5 to 23 and the minute, 40
, is divisible by 20. It will not run at 00:00 because the hour, 00
, is not in the range 5 to 23.
@Alex's answer is correct, however it took me a while to find a source.
The answer is in man crontab.5
(or also info crontab
) on Debian, Mac OS X, FreeBSD (and other Posix systems):
Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an ``hours'' entry specifies execution at hours 8, 9, 10 and 11.
For my application I wanted a script to run every 5 minutes during business hours (9am - 5pm) and another to run every 5 minutes outside of that. Unfortunately the ranges can't wrap across midnight, so you need to specify 3 ranges (morning, business hours, evening)
*/5 0-8,17-23 * * * outside-hours.sh
*/5 9-16 * * * business-hours.sh
This should run
outside-hours.sh first at 00:00 and finally at 08:55
business-hours.sh first at 09:00 and finally at 16:55
outside-hours.sh first at 17:00 and finally at 23:55
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With