linuxcronsystem-administration

Crontab Day of the Week syntax


In crontab does the Day of the Week field run from 0 - 6 or 1 -7?

I am seeing conflicting information on this. wikipedia states 0-6 and other sites I have seen are 1-7.

Also what would be the implication or either using 0 or 7 incorrectly? i.e. would the cron still run?


Solution

  • 0 and 7 both stand for Sunday, you can use the one you want, so writing 0-6 or 1-7 has the same result.

    Also, as suggested by @Henrik, it is possible to replace numbers by shortened name of days, such as MON, THU, etc:

    0 - Sun      Sunday
    1 - Mon      Monday
    2 - Tue      Tuesday
    3 - Wed      Wednesday
    4 - Thu      Thursday
    5 - Fri      Friday
    6 - Sat      Saturday
    7 - Sun      Sunday
    

    Graphically, * * * * * command to be executed stands for:

    minute hour day of month month day of week
    (0-59) (0-23) (1-31) (1-12) (1-7)
    * * * * * command to be executed

    Or using the old style:

     ┌────────── minute (0 - 59)
     │ ┌──────── hour (0 - 23)
     │ │ ┌────── day of month (1 - 31)
     │ │ │ ┌──── month (1 - 12)
     │ │ │ │ ┌── day of week (0 - 6 => Sunday - Saturday, or
     │ │ │ │ │                1 - 7 => Monday - Sunday)
     ↓ ↓ ↓ ↓ ↓
     * * * * * command to be executed
    

    Finally, if you want to specify day by day, you can separate days with commas, for example SUN,MON,THU will exectute the command only on sundays, mondays on thursdays.

    You can read further details in Wikipedia's article about Cron and check a cron expression online with crontab.guru.