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
How to run a crontab job every week on Sunday?
In order to create a crontab job to run every Sunday, we first need to explore and understand what a crontab job is.
A crontab is nothing but a list of commands that we can run during a cron job. A cron job is a utility that schedules automatic execution of commands at specific times.
Creating a Crontab Job
We can start a cron job with the help of bash script by following the commands shown below −
crontab -e
This will open a file which you can edit, insert the cron job shell script in the above file and then close that file.
Crontab Syntax
Just insert the code shown below in the above file
* * * * * sample.sh
The above command contains 5 asterisks, where each * indicates the time field and then follows the script. We have the script which we want to run as a cron job. In the sample.sh we need to write the following command to make the environment variables available to it.
Now we understand how we can create a crontab job, it is time to understand what these * actually mean and how we can replace their values to solve our particular question.
Cron Time Fields
The five asterisks in the above command actually have a separate meaning attached to them. These mainly mean −
| Field | Position | Range | Description |
|---|---|---|---|
| Minute | 1 | 0-59 | Minute of the hour |
| Hour | 2 | 0-23 | Hour of the day (24-hour format) |
| Day of Month | 3 | 1-31 | Day within the month |
| Month | 4 | 1-12 | Month of the year |
| Day of Week | 5 | 0-7 | Day of the week (0 and 7 = Sunday) |
Running Jobs Every Sunday
If we want to run a crontab job every Sunday then we have three possible combinations that we can run. These are −
5 8 * * 0 5 8 * * 7 5 8 * * Sun
The 5 8 in the above crontab job command stands for the time of the day when this will happen: 8:05 AM.
Examples
It should be noted that we need to append our script that we need to run after any of the above commands we choose.
The final command should look something like this −
5 8 * * 0 /path/to/script.sh
Here are more examples for different times on Sunday −
# Run at midnight every Sunday 0 0 * * 0 /path/to/backup-script.sh # Run at 2:30 PM every Sunday 30 14 * * Sun /path/to/weekly-report.sh # Run every 30 minutes on Sundays only */30 * * * 7 /path/to/monitor.sh
Key Points
Sunday can be represented as
0,7, orSunAlways use absolute paths for scripts to avoid execution issues
Use
crontab -lto list existing cron jobsUse
crontab -rto remove all cron jobs for the current user
Conclusion
Running a crontab job every Sunday requires setting the day of week field to 0, 7, or Sun while specifying the desired time. This allows for automated weekly tasks like backups, reports, or system maintenance to run reliably every Sunday.
