cronie-crond / cronie

Cronie cron daemon project
Other
453 stars 77 forks source link

step in crontab doesn't function as expected when it isn't an even divisor of range #142

Closed jikamens closed 1 year ago

jikamens commented 1 year ago

If I specify */59 * * * * in a crontab, then I expect the job to run every 59 minutes, with minute of the time the job executes decreasing by one minute every hour. For example, at some point the sequence of run times might be 13:02, 14:01, 15:00, 15:59, etc.

Instead, what happens is that the job runs every :00 and :59 of the hour, which means that it runs twice per hour, one minute apart.

This is, at best, unexpected behavior given what it says in the man page, and the man page should be updated to reflect it. At worst, it's a bug. I, personally, would certainly prefer for this to work as described in the first paragraph.

t8m commented 1 year ago

This implementation was there basically forever and I do not think changing it at this point makes sense. So yeah, this is a documenation issue.

jikamens commented 1 year ago

Yeah, I took a look at the code when submitting this and I figured that's what you would say. ;-) Certainly too much work / disruption to change the behavior.

Having said that, is somewhere in the distribution, either the man page or somewhere else, where it would be appropriate to give examples of how to implement the desired frequency? Something like this:

* * * * * if [ $(expr \( $(date +\%s) / 60 \) \% 58) = 0 ]; then echo this runs every 58 minutes; fi
* * * * * if [ $(expr \( $(date +\%s) / 3600 \) \% 23) = 0 ]; then echo this runs every 23 hours; fi

ETA: I originally tried to use + $RANDOM_DELAY in this example but that apparently doesn't work? Not sure why.

t8m commented 1 year ago

The second example is still not correct - You should use something like 30 * * * * .... otherwise it would be run for every minute in every 23rd hour.

Not sure what would you like to achieve with $RANDOM_DELAY as that would be a fixed value anyway.

I think it could be placed somewhere in crontab(5) in NOTES section perhaps.

jikamens commented 1 year ago

The second example is still not correct - You should use something like 30 * * * * .... otherwise it would be run for every minute in every 23rd hour.

Good catch, thanks. New examples:

* * * * * if [ $(expr \( $(date +\%s) / 60 \) \% 58) = 0 ]; then echo this runs every 58 minutes; fi
0 * * * * if [ $(expr \( $(date +\%s) / 3600 \) \% 23) = 0 ]; then echo this runs every 23 hours on the hour; fi

Not sure what would you like to achieve with $RANDOM_DELAY as that would be a fixed value anyway.

Upon further reflection I think I misunderstood what RANDOM_DELAY is. I thought it was set to a static random number I could add to offset the modulus calculation, but it appears that it's a variable that the user sets to affect scheduling behavior, not a variable that crond sets that the user can use in their rules.

I think it could be placed somewhere in crontab(5) in NOTES section perhaps

Sounds reasonable. Would you take a PR if I submit one?

t8m commented 1 year ago

Sounds reasonable. Would you take a PR if I submit one?

yes, sure