Updated April 14, 2023
Introduction to Cron Weekly
Cron weekly is the facility which helps us to schedule the jobs or tasks that need to be executed periodically every week. A daemon thread runs in the background to continuously check if the current week matches with the upcoming week that has been scheduled. After executing the job on a particular week, the week on which the task is to be executed again will automatically be increased so that the daemon thread can check and match that week with the current week each time and run the job accordingly on each week.
What is Cron Weekly?
Cron weekly are the special jobs which need to execute only once in each of the week. In order to understand how we can create a cron job that executes weekly, let us firstly understand what cron tab is and how we can use the command in crontab to create a weekly cron job. Cron tab is the file that contains a list of commands that are cron jobs that are created and scheduled in the file. The cron weekly job can be created by using either of the two available ways, one which contains weekly specification in date and time numbers or weekly string.
Each of the line in the cron tab contains 5 fields related to time and date. After those 5 fields, a command is present which is to be executed, such as backing up the data, executing some script or php file, or any other synchronization work between two tasks. There after a new line character is entered. Note that all this fields are separated from one another using a space.
The 5 fields of time and date are given below:
- A minute can be represented starting from 0 to 59.
- An hour can be specified by specifying a number between 0 to 23, where 0 will be midnight.
- Day can be represented by using 1 to 31 numbers.
- For a month, be can write numbers between 1 to 12.
- Lastly, week days such as Sunday, Monday, etc., can be specified by using 0 to 6 numbers where 0 stands for Sunday.
Other than the above ones, one special string for weekly cron jobs contains “0 0 * * 0,” which will indicate that we want to execute that cron job once in a week.
Cron Weekly List
We can check the complete list of all the cron jobs that the root user schedules by using the command.
The execution of the above command will retrieve all the cron jobs that the root user creates. In case if you want to display the cron jobs that a particular user creates, we will also need to mention the name of the user (username) in the command whose syntax is as shown below.
Crontab username -l
For checking the list of the cron jobs scheduled on a weekly basis that is executed once per week, you can type in the following commands.
Navigate to the cron.weekly file present inside the etc/ directory by using the below command.
Next, type in the ls command with the -l option.
The next and last thing to do is run the cat command to create a file with a particular name where the list of those jobs will be saved after creating a new file with that name.
For example, if we want the folder to be created with the name educba, we can enter the command below.
How to Use Cron Weekly?
- We can use cron weekly, usually when you have to take the backups because the backup of the system is to be taken is usually odd, such as 2 am in the night or 12 am at midnight when the users are not using the system. But at that time, it’s not possible for a technical person to be present and initiate the command of backup and execute the same. This can be easily managed by just creating the cron job once weekly, which will then execute each week, and backup operation will be performed automatically.
- Other than the above application, cron jobs can also be used for setting the operating system time as most of the time, using the NTP that is Network Time Protocol can take little time and often causes a drift. That is why we can make the setting of hardware time by using cron and system time.
- One more application where we can use cron jobs in services such as iogrotate, logwatch, and rootkit hunter for task scheduling and executing the programs on a daily basis.
Examples of Cron Weekly
Given below are the examples of cron weekly:
For example, suppose that we have one task of executing a file located in the directory /etc/ EDUCBA/ cronjob/ myTask.sh once in a week using cron jobs, then we can write the command as shown below.
0 0 * * 0 /etc/ EDUCBA/ cronjob/ myTask.sh
Or other way of specifying the same command using a string is as shown here.
@weekly /etc/ EDUCBA/ cronjob/ myTask.sh
Note that the above command can only execute and run each week you have your system started and running.
The cron weekly is the utility available in Unix/ Linux platforms where we can schedule a particular cron job which needs to execute once per week. We can create that cron jobs by using crontab and list them using the crontab -l command. While creating, we can specify the date and time-related fields for the week or optionally a string that is provided of @weekly in the command for creating a cron weekly job.
We hope that this EDUCBA information on “Cron Weekly” was beneficial to you. You can view EDUCBA’s recommended articles for more information.