Cron Scheduling – When Will an Interval Execute First Time?

cronscheduling

tl;dr: Does cron use the numerical value of an interval compared to the numerical value of the day to determine its time of execution or is it literally "every 3 days" at the prescribed time from creation?

Question:

If I add the following job with crontab -e will it run at midnight tomorrow for the first time or three days from tomorrow? Or is it only on "third" days of the month? Day 1, 4, 7, 10…?

0 0 */3 * * /home/user/script.sh

I put this cron in yesterday and it ran this morning (that might be the answer to my question) but I want to verify that this is correct. Today is the 31st and that interval value does appear to fall into the sequence. If cron starts executing an interval on the 1st of the month, will it run again tomorrow for me?

Additional notes:

There are already some excellent posts and resources about cron in general (it is a common topic I know) however the starting point for a specific interval isn't as clear to me. Multiple sources word it in multiple ways:

  • This unixgeeks.org post states:

    Cron also supports 'step' values.
    A value of */2 in the dom field would mean the command runs every two days
    and likewise, */5 in the hours field would mean the command runs every
    5 hours.

    • So what is implied really by every two days?
  • This answer states that a cronjob of 0 0 */2 * * would execute "at 00:00 on every odd-numbered day (default range with step 2, i.e. 1,3,5,7,…,31)"

    • Does cron always step from the first day of the month?
    • It appears that the blog states the cron will execute on the 31st and then again on the 1st of the next month (so two days in a row) due to the interval being based on the numeric value of the day.
  • Another example from this blog post

    • 0 1 1 */2 * command to be executed is supposed to execute the first day of month, every two months at 1am
    • Does this imply that the cron will execute months 1,3,5,7,9,11?

It appears that cron is designed to execute interval cronjobs (*/3) based on the numerical value of the interval compared to the numerical value of the day (or second, minute, hour, month). Is this 100% correct?

P.S. This is a very specific question about one particular feature of cron that (I believe) needs some clarification. This should allow Google to tell you, with 100% certainty, when your "every 3 months" cron will run for the first time after it's been added to crontab.

Best Answer

The crontab(5) man page use a wording that is pretty clear:

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 is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".

The exact wording (and the example) is "skips of the number's value through the range" - and it is implied that it starts at the first number in the range.

This mean if the range is 1-31 for days, the values returned in the case of 1-31/2 or */2 is 1,3,5,7.. etc. This also means that the range is reset to the start value when it has run through.

So you are also correct that in this case, the cronjob would run both on the 31th and 1st the month after.

Please note that cron has 2 fields that are mutually exclusive - the "day of month" and "day of week". So you have to choose one or the other, when running jobs with an interval of days.

If you want to define a cronjob that runs perfectly every other day, you have to use multiple lines and custom define each month according to the current calendar.

Related Question