Linux is an operating system that is widely used and comes with various versions known as distributions. One such distribution is the Ubuntu which is widely used and is popular among the users also. In order to manage the backup tasks and maintain the software there is a need for a system. Therefore, it is done by Cron. It manages the tasks that require daily upgradation and also schedules the jobs accordingly.
The following aspects of Crontab in Linux will be covered in detail in this article.
- What is crontab?
- How Does Crontab Work?
- Crontab Syntax
- Setup the Cron Job
- Examples of Crontab
- Conclusion
What is crontab?
Cron is basically a system software that is used in Unix-like systems that are used to schedule jobs. It is mostly used by the people who maintain the software. The jobs that are referred to as cron jobs can be scheduled according to day, time, and certain intervals. These jobs then run according to the designed pattern by the Operating system.
How Does Crontab Work?
Cron is a text file that contains the commands to run the jobs at specified times. The system administrators are allowed to edit the crontab files only. The crontab is useful for private servers that run 24/7. In Windows, the Services are termed background jobs which is similar to crontab.
Crontab Syntax
The crontab follows the syntax as per the sequence mentioned below:
- Minute: The range of this parameter is from 0 to 59.
- Hour: Following the 24-hour format, the range of this particular parameter is from 0 to 23.
- Day of the Month: The command that can be run from 1 to 31 in a month.
- Month: The range of the month is 1 to 12.
- Day of the Week: This ranges from 0 to 6 which includes Saturday and Sunday. In some cases, Sunday is represented by 7.
The * wildcard represents all in crontab syntax when the job is to be scheduled.
Setup Cron Job
The steps depicted below help to run the cron job in an easy manner along with the examples.
First, run the command as depicted below to check for updates.
sudo apt update
.
Now install the cron using the following command:
sudo apt install cron
In the next step, the crontab can be enabled using the command below to start working with cron.
sudo systemctl enable cron
When the crontab -e command is executed in the terminal the option appears for the selection of editor since option 1 is the easiest therefore, it is selected.
crontab -e
The editor appears in the next step and you can execute the commands easily using this editor.
Some example commands are also listed below which can be executed.
Crontab Examples
The examples below show how crontab is used in different ways.
Example 1: Creating a New File
Let us create a file that appears at the given timespan. For this, we will use the syntax and name a file as shown in the screenshot below:
10 17 * * * /bin/sh /home/user/Desktop/filenew.sh
After this save and quit using ctrl+X, the terminal will open then change the directory using the cd command to desktop and use Vim command to publish the file on the Desktop.
Use the Esc and:wq to come out of the window. The file will be executed at the time entered as depicted below in the screenshot.
Example 2: Script To Run Every Minute
The below command shows that the file is declared as a script.sh, which will run every minute. For this example, the content is not added to the file which will later be added.
* * * * * /bin/sh /home/user/Desktop/script.sh
Example 3: Run String Every Minute
The below command runs Hello for every minute which is declared in the file as welcome.txt.
* * * * * echo “Hello” home/user/Desktop/welcome.txt
The output is attached below when the file is executed.
Example 4: Check Disk Space
The following command checks for the disk space every 2 minutes.
*/2 * * * * home/user/Desktop/check-disk-space
Example 5: Execution of File at 9 pm Daily
This command allows the file to execute at 9 pm every day.
* 9 * * * /bin/sh /home/user/Desktop/file1.sh
Example 6: Run the Script on Specified Time
The command below will run the script every December 31st at 3:15 pm.
15 3 31 12 * /home/user/bin/checkdrive.sh
Example 7: Run the Script on Weekend
This command helps to run the script every weekend which is Saturday(6) and Sunday(7).
* * * * 6,7 /home/user/Desktop/file1.sh
Example 8: Run the Script Once a Year
This command is responsible for running file1.sh script once a year only that is on the 1st of January.
0 0 1 1 * /home/user/Desktop/file1.sh
Example 9: Run the Script With Interval
This command will run the script with 5 minutes of interval.
*/5 * * * * /home/user/Desktop/file1.sh
Example 10: Yearly Command
This command can be used to send New Year greetings which is similar to 0 0 1 * *.
@yearly /home/user/Desktop/newyear.sh
This concludes the working of the cron jobs and cron tabs in detail.
Conclusion
Crontab is basically a list of commands that are used to schedule the jobs according to the preference. It contains the list of commands which help the execution of scheduled jobs. In this article, we have studied the details regarding what is crontab its syntax, and the examples related to it.
Leave feedback about this