Does Crontab Day Of The Week Run From 0-6 Or 1-7?

Published July 27, 2024

Problem: Crontab Day of Week Numbering Confusion

Crontab's day of week field can be confusing because of different numbering systems. Some systems use 0-6 (Sunday to Saturday), while others use 1-7 (Monday to Sunday). This difference can cause scheduling errors if not understood correctly.

The Answer: Both 0-6 and 1-7 Are Correct

Crontab's Day of Week Representation

Crontab allows you to represent days of the week in two ways. You can use 0-6 or 1-7 for crontab schedules. This system lets you use the numbering method you prefer.

  • 0 and 7 mean Sunday: Remember this point. Crontab sees both 0 and 7 as Sunday.
  • You can use 0-6 or 1-7: Both ranges work in your crontab entries.

Tip: Use Day Names for Clarity

For better readability, you can use three-letter abbreviations for days (SUN, MON, TUE, etc.) instead of numbers in your crontab entries. This makes your schedules easier to understand at a glance.

Day of Week Numbers

Here's how numbers match days of the week in crontab:

  • 0/7 - Sunday
  • 1 - Monday
  • 2 - Tuesday
  • 3 - Wednesday
  • 4 - Thursday
  • 5 - Friday
  • 6 - Saturday

This system lets you use one number for each day. For example, to set a task for every Wednesday, use "3" or "WED" in the day of week field of your crontab entry.

Knowing this numbering system helps you avoid mistakes and schedule cron jobs better.

Alternative Day Representations in Crontab

Using Day Name Abbreviations

Crontab lets you use three-letter abbreviations for days of the week. This can make your crontab entries easier to read and less likely to have errors. The valid abbreviations are:

  • SUN for Sunday
  • MON for Monday
  • TUE for Tuesday
  • WED for Wednesday
  • THU for Thursday
  • FRI for Friday
  • SAT for Saturday

You can use these abbreviations instead of numbers in the day of week field of your crontab entry. For example, you can write "MON" instead of "1" for Monday. This is helpful when making complex schedules or when you need to understand a crontab entry quickly without looking up the numbering system.

Using day name abbreviations also helps avoid confusion between the 0-6 and 1-7 numbering systems. It gives a clear way to specify days, no matter which numbering system you're used to.

Remember that these abbreviations are case-sensitive, so always use uppercase letters when specifying days this way in your crontab entries.

Example: Scheduling a Weekly Backup

0 2 * * MON /usr/local/bin/backup_script.sh

This crontab entry schedules a backup script to run every Monday at 2:00 AM. Using 'MON' instead of '1' makes it clear that the job runs on Mondays without needing to remember the day number.

Crontab Schedule Format Breakdown

The Five Fields of a Crontab Schedule

Crontab schedules use five fields to set the timing for tasks. Each field stands for a time unit:

  1. Minute: Values from 0 to 59.
  2. Hour: Numbers from 0 to 23.
  3. Day of Month: Values from 1 to 31.
  4. Month: Numbers from 1 to 12.
  5. Day of Week: Numbers 0-6 or 1-7.

These fields appear in the order above when you create a crontab entry. A space separates each field. A basic crontab entry looks like this:

* * * * * /path/to/command

Each asterisk (*) is a field. The asterisk means "every" for that time unit. This example would run the command every minute of every hour, every day.

You can use specific values or ranges to set more precise schedules. For example:

30 2 * * 1-5 /path/to/command

This entry would run the command at 2:30 AM, Monday through Friday, every week.

Knowing these five fields helps you create accurate crontab schedules for your tasks.

Tip: Using Step Values in Crontab

You can use step values in crontab schedules to run tasks at regular intervals. The format is */n, where n is the interval. For example:

*/15 * * * * /path/to/command

This would run the command every 15 minutes.

Best Practices for Day of Week Specification in Crontab

Consistency in Crontab Entries

When working with crontab, follow best practices for specifying days of the week. This helps keep schedules clear and error-free.

Choose one numbering system and use it consistently. Decide if you prefer 0-6 or 1-7 for representing days of the week, and stick to that choice across all your crontab entries. This makes your schedules easier to read and maintain.

For example, if you choose the 0-6 system:

0 9 * * 0 /path/to/sunday_script.sh
0 9 * * 1 /path/to/monday_script.sh

Or if you prefer the 1-7 system:

0 9 * * 7 /path/to/sunday_script.sh
0 9 * * 1 /path/to/monday_script.sh

Use day name abbreviations when you need more clarity. While numbers work well, using SUN, MON, TUE, etc., can make your schedules easier to understand at a glance. This is helpful for complex schedules or when sharing crontab files with others.

Here's an example using day name abbreviations:

0 9 * * SUN /path/to/sunday_script.sh
0 9 * * MON /path/to/monday_script.sh