Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Crontab day of the week syntax on Linux
A crontab is a configuration file that contains a list of commands scheduled to run at specific times. It uses the cron daemon, a time-based job scheduler in Unix-like operating systems, to execute these commands automatically.
Understanding Crontab Syntax
To create or edit a crontab job, use the following command:
crontab -e
This opens the crontab editor where you can add scheduled jobs. Each crontab entry follows a specific format with five time fields followed by the command to execute:
* * * * * command_to_execute
Crontab Time Fields
The five asterisks represent different time units in the following order:
| Position | Field | Range | Description |
|---|---|---|---|
| 1 | Minute | 0-59 | Minutes past the hour |
| 2 | Hour | 0-23 | Hour of the day (24-hour format) |
| 3 | Day of Month | 1-31 | Day of the month |
| 4 | Month | 1-12 | Month of the year |
| 5 | Day of Week | 0-7 | Day of the week |
Day of Week Syntax
The day of week field accepts both numeric and text values:
| Numeric Value | Abbreviation | Full Name |
|---|---|---|
| 0 | Sun | Sunday |
| 1 | Mon | Monday |
| 2 | Tue | Tuesday |
| 3 | Wed | Wednesday |
| 4 | Thu | Thursday |
| 5 | Fri | Friday |
| 6 | Sat | Saturday |
| 7 | Sun | Sunday (alternative) |
Note: Both 0 and 7 represent Sunday, providing flexibility in scheduling Sunday jobs.
Examples
Running a Job Every Sunday
To schedule a script to run every Sunday at 8:05 AM, you can use any of these formats:
5 8 * * 0 /path/to/script.sh 5 8 * * 7 /path/to/script.sh 5 8 * * Sun /path/to/script.sh
Other Day-Specific Examples
# Run backup every Monday at 2:30 AM 30 2 * * 1 /home/user/backup.sh # Execute maintenance every Friday at 11:00 PM 0 23 * * Fri /usr/local/bin/maintenance.sh # Send reports every Wednesday at 9:15 AM 15 9 * * Wed /opt/reports/generate_report.py
Advanced Day Scheduling
You can also use ranges and lists for more complex scheduling:
# Run Monday through Friday (weekdays) 0 9 * * 1-5 /path/to/weekday_task.sh # Run on Monday, Wednesday, and Friday 0 18 * * 1,3,5 /path/to/selected_days.sh # Run every other day of the week 0 12 * * */2 /path/to/every_other_day.sh
Conclusion
Understanding crontab day-of-week syntax is essential for scheduling automated tasks on specific days. The fifth field accepts values 0-7 (with both 0 and 7 representing Sunday) or three-letter abbreviations, providing flexible options for day-based job scheduling in Linux systems.
