315

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?

3 Answers 3

570

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.

Sign up to request clarification or add additional context in comments.

7 Comments

ok thats what i didnt realise Sunday can be a 0 or 7. thanks
Be aware that you cannot do: Sat-Sun, only 6-7 or Sat,Sun
Can you confirm this is case-insensitive? You switch a few times among SUN, Sun, Sunday in your response
@MichaelChirico oh, good point. Checking Wikipedia's article about Cron I read The month and weekday abbreviations are not case-sensitive.
name of day can also be entirely lower case, e.g: wed
|
141
 :-) Sunday | Sun -> 0 | Monday | Mon -> 1 Tuesday | Tue -> 2 Wednesday | Wed -> 3 Thursday | Thu -> 4 Friday | Fri -> 5 Saturday | Sat -> 6 | :-) Sunday | 7 

As you can see above, the numbers 0 and 7 are both assigned to Sunday. There are also the English abbreviated days of the week listed, which can also be used in the crontab.

Examples of Number or Abbreviation Use

15 09 * * 5,6,0 command 15 09 * * 5,6,7 command 15 09 * * 5-7 command 15 09 * * Fri,Sat,Sun command 

The four examples do all the same and execute a command every Friday, Saturday, and Sunday at 9.15 o'clock.

In Detail

Utilising both 0 and 7 to represent Sunday is advantageous for creating weekday ranges[*] that start or end with Sunday, such as 0-2 or 5-7. Ranges must start with the lower number and finish with the higher number. Abbreviations like Sun, Mon, Tue, etc. can also be used. But Sun is not allowed to be set at the end of the range. For example, you cannot shorten Fri,Sat,Sun to Fri-Sun.


[*] In the context of a crontab, a range is used to specify a continuous sequence of time units, such as minutes, hours, days, or weekdays. Ranges in crontab are represented using a hyphen-minus character (-) between the lower and upper bound values. For instance, a range of weekdays from Monday (1) to Wednesday (3) would be represented as 1-3.

4 Comments

For clarity, “The abbreviations cannot be used to define a weekday range.” statement here seems to be a way of saying that one can’t separate abbreviations by hyphen‐minus characters, only commas. In other words, Fri,Sat,Sun can’t be shortened to Fri-Sun.
@PatrickDark, I edited the answer, hopefully making it more understandable.
@PatrickDark But in my crontab mon-fri works, I use it on debian. Also works thu-sat but thu-sun does not work. Why is it possible that thu-sun does not work but thu-sat works?
@T0maas I guess Sun is handled like 0, it can only be placed at the beginning as the first day. If you want to set Sunday as the last day, you can use the numbers and use 7 for Sunday.
15

You can also use day names like Mon for Monday, Tue for Tuesday, etc. It's more human friendly.

2 Comments

This won't work for some distributions; testing with Ubuntu 14.04.3 LTS, I got "/tmp/crontab.Nuq9GE/crontab":24: bad day-of-week"
@Nikita check for typos or extra spaces. I have both a "Mon-Fri" and a "Thu" in my crontab on Ubuntu 14.04.3 and both work just fine. I suspect if there's a space between any of the characters, you'd end up with an error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.