26

Is this valid crontab time specification, doing what is expected:

0 22-4 * * *

Or is it necessary to do something like

0 22,23,0,1,2,3,4 * * *
hyde
  • 1,288
  • 1
  • 13
  • 20

4 Answers4

15

I had problems running cronjobs over midnight. I ended up putting in two almost identical entries for the scripts involved.

* 22-00 * * * /myscript
* 00-05 * * * /myscript

that seemed to do the trick.
I was experiencing cron failures because of using 22-05, and by process of elimination came up with this solution.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
user73420
  • 151
11

There are a wide range of cron daemons around, unless you tell which exact one you are using (the handling of lists in such corner cases could very well change from a version to the next) there can't be an answer. Best bet is to go with your explicit list. I wouldn't be too surprised if some implementation takes your wrap-around as a weird way of saying 4-22...

Does your local documentation even mention this sort of case? Maybe it is legal to have two lists, e.g. 22-23,0-4?

Kusalananda
  • 333,661
vonbrand
  • 18,253
5

I've never attempted to use a range like that, and I'm not sure whether it would work. So my first advice would be to test it and see what happens - though probably with a script that only does a log entry or something else innocuous.

Second, for ATT and BSD cron you can't have ranges and lists co-existing, so there you'd either have to list each hour separately or have two lines, one with the range and one with the list.

Jenny D
  • 13,172
  • Modified question to be valid on ATT/BSD by your answer. I'm actually writing "my" crontab spec in Jenkins job configuration (and will test that tonight with dummy job), but I'm asking here to know what is expected behaviour on Unix/Linux system. – hyde Mar 07 '13 at 09:53
  • Actually I'd expect it to not work and be pleasantly surprised if it did. But I'd also expect cron to complain if you enter an invalid range. – Jenny D Mar 07 '13 at 10:06
2

Useful tool to check your cron:

https://crontab.guru/

Using the cron * 22-4 * * * won't work as cron works in "current day".

You can try different variations of it, but the most precise in my view is this one:

enter image description here

Mario
  • 121
  • A more usable version would be 0 0-4,22-23 * * * which affects hours 0,2,3,4,22,23 on every given day. – Martin Jun 20 '22 at 10:42