Issue
If I set up my cron to be:
* */6 * * *
Then my understanding is, that it will run at every day at every hour that is divisible by 6, so:
06:00
12:00
18:00
24:00
Suppose I would like it to run it with a delay like 1 hour [pseudocode]:
* */6+1 * * *
or
06:00 + 01:00 = 07:00
12:00 + 01:00 = 13:00
18:00 + 01:00 = 19:00
24:00 + 01:00 = 01:00
How would I do that?
If I make it */7
:
* */7 * * *
It will run:
07:00
14:00
21:00
which is not what I am looking for.
Solution
There are a few different implementations of cron
, so check the manual on your system by running man 5 crontab
to confirm the format it supports.
Assuming you have the popular "Vixie cron", the format description includes the following:
Step values can be used in conjunction with ranges. Following a range with
/<number>
specifies skips of the number's value through the range. For example,0-23/2
can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is0,2,4,6,8,10,12,14,16,18,20,22
). Steps are also permitted after an asterisk, so if you want to sayevery two hours
, just use*/2
.
And above that:
A field may be an asterisk (*), which always stands for
first-last
.
So, */6
is actually short-hand for 0-23/6
- start at 0
, and continue in steps of 6 until we reach 23
, giving 0,6,12,18
. (Note that the hour field runs 0 to 23, not 1 to 24, as you've implied in the question.)
In this implementation, therefore, the sequence 1,7,13,19
can be written 1-23/6
(or 1-19/6
).
With a large skip, writing out the list in full (1,7,13,19
) is arguably more readable, and should work on all versions of cron
, but that's up to you.
Answered By - IMSoP Answer Checked By - Gilberto Lyons (WPSolving Admin)