Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the following cron expression means every 45 minutes?

Tags:

cron

Am willing to run a script every 45 minute (not the :45th minute of every hour)

e.g. 10:00, 10:45, 11:30, 12:15, and so on.

*/45 * * * *

Am not sure this is the correct expression.

like image 985
Tzury Bar Yochay Avatar asked Jan 25 '13 08:01

Tzury Bar Yochay


1 Answers

I suspect (edit: I'm pretty sure by now) that it doesn't do what you want: fields are separate, and */45 for minutes is nothing more than 0,45. I would use the following three entries if */45 doesn't do the job:

0,45  0-23/3 * * *
30    1-23/3 * * *
15    2-23/3 * * *

If you take a look at entry.c file in vixie cron sources, you'll notice that each field of each entry is parsed by get_list and represented as bitmaps of allowed values for that field. That almost precludes any "smart" interpretation, as the distinction of */45 and 0,45 is lost at this stage... but there is a MIN_STAR flag, set at the presence of * in minutes (including */45). So we take a look at cron.c, a single place where MIN_STAR is examined, to learn it's unrelated to our problem. Now we know for sure that */45 means "every 45th minute of every hour": 0:00, 0:45, 1:00, 1:45 and so on.

There were two answers here confidently stating the opposite, quoting an unfortunate passage in the manual:

Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2"

We are lucky to have a 24 hour day, containing even number of hours, making "every two hours from 0:00, each day" and "every two hours generally" indistinguishable. Too bad that the manual didn't go far enough to document non-trivial cases, making the impression that * */22 means every 22 hours. It does not. Star with a step is just a shorthand for a list of values in the field where it's used; it doesn't interact with other fields.

like image 65
Anton Kovalenko Avatar answered Oct 26 '22 09:10

Anton Kovalenko