16

cronie 1.5.2 on RHEL 8.10

This entry runs foobar every 30 minutes from 13:00 to 17:30.

*/30  13-17  *  *  1-5  foobar.sh

Is there some clever (but not hackish, like adding "exit if it's 17:30" to the script) way to get it to not run at 17:30, or is brute force (two crontab entries) the best way?

*/30  13-16  *  *  1-5  foobar.sh
0     17     *  *  1-5  foobar.sh
1
  • 6
    well, the hour of 17:xx is different in that logic as compared to 13:yy and others (because you run when yy = 00 or yy = 30, but only when xx = 00), so making it a separate rule seems fitting enough. And what you have there should be clear enough to a reader (though you could add a comment of you like), so probably no need to think of it too hard :) Commented Jun 1 at 20:50

1 Answer 1

29

You have it right. If you want it to run every half hour from 13:00 until 17:00 but not at 17:30, I would write the cron entries like this:

# foobar.sh runs weekdays at the top of the hour from 13:00 through 17:00
# it also runs weekdays at the bottom of the hour from 13:30 through 16:30 (not 17:30)
0   13-17  *  *  1-5  foobar.sh
30  13-16  *  *  1-5  foobar.sh

IMO comments are key to preventing someone from misunderstanding the crontab entries. (that someone is often yourself a few months after the implementation, when many of the details are forgotten)


(I originally had example shell script code that would detect being invoked at 17:30 and exit. However the question specifically excluded this approach, and a number of criticisms were also made in comments, so I've removed the example as not relevant to the question)

13
  • 2
    @ilkkachu yes, that functions, but I see that as less maintainable than the simpler techniques in my answer. At the jobs where I've faced these kinds of implementation questions, I've learned the clever-but-unusual solutions confuse my teammates more than the simple solutions, and that confusion leads to updates that break things. Comments/documentation help avoid that, which is why I recommend them in most of my answers (such as this one). Commented Jun 2 at 3:48
  • 8
    The OP is running RHEL and the question is tagged Linux. The Bourne shell and Forsyth shells were the only Bourne-like shells that didn't have $(...) and have never been used on Linux. Other than on Solaris (up to 10) and some now defunct and anecdotal systems, the Bourne shell hasn't been in use since the 90s. Commented Jun 2 at 7:25
  • 9
    Having part of the conditions in crontab and part in the script is a maintenance issue. Too easy to later edit the 13-17 to 14-18, which makes it skip 17:30 but run 18:30. Commented Jun 2 at 7:45
  • 3
    @ilkkachu using a bare % in a crontab line will fail Commented Jun 2 at 7:47
  • 5
    That time test in the script is potentially quite fragile, something like "greater than 17:29" would be better, if you ever have to go that way, just in case something delays execution until 17:31 Commented Jun 2 at 9:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.