Cron is an effective and popular command-line utility used to schedule a broad range of tasks at a specified time without user interaction. The scheduled tasks are known as cron jobs while the crontab is the list of the file containing the cron jobs.
In this article, we will explain with various example shows you can edit and use crontab file in order to schedule tasks in Debian 10 system.
Cron can be used to automate tasks in the following scenarios:
- Running schedule backups,
- Checking disk space at a specific interval,
- Running automatic maintenance,
- Periodically removing unwanted files,
- Running network diagnostic
Syntax
It is easier to work with cron, once you understand its syntax. The general syntax to use cron is:
Each of the fields corresponds to the following:
Day_of_the_Week (0–6) command
Installing Cron
Cron comes preinstalled in Debian 10. However, if it is missing from your system, run the following command to install it:
View crontab
To view crontab file, use the following command:
To view crontab file of a particular user, use the following command:
Edit crontab
To edit the crontab file of the current user:
To edit the crontab file of a particular user:
If you are editing crontab file for the first time, you will be asked to choose an editor:
Once you select the editor, the following window will appear.
For instance, you want to run a backup script named “backup.sh” located at documents directory daily at 2:00 am. To do so, you will need to add the following line in your crontab file:
In the above window, you can add your cron jobs one by one. Once done, save and exit the file after that all the cron jobs will automatically run at their specified interval.
Remove crontab
To Remove the crontab file, enter the following command in Terminal:
Now lets go through a series of examples of cron jobs on a linux machine.
1. Run a cron job every minute
To execute a cron job every minute, add the following line in your crontab file:
2. Run a cron job after every 10 minutes
To schedule a cron job to execute after every 10 minutes, add the following line in your crontab file.
3. Run a cron job on specific months
To schedule a cron job to execute on specific months e.g. March, august, December, add the cron job in your crontab file in the following way:
4. Run a cron job on selected days
If you would like your cron job to be executed on selected days let’s suppose Sunday and Friday at 5 PM, it should look like the following.
5. Run a cron job daily at a specific time
To schedule a cron job to execute daily at a specific time, let’s say at 3 am, add this line in your crontab file.
6. Run a cron job on a specific day
To schedule a cron job to execute on a specific day, let’s say every Sunday, add this line in your crontab file.
The job will run at 00:00 on every Sunday.
7. Run multiple tasks in a single cron job
To schedule multiple tasks in a single cron job, add all scripts in your crontab file by separating them with a semicolon.
8. Run a cron job on every weekday
To schedule a cron job to execute on every day of the week that is from Monday to Friday, you will need to add this line in your crontab file.
The job will run at 00:00 on every weekday.
9. Run a cron job twice a day
To schedule a job to run two times a day e.g. at 6 am and 3 pm, you will need to add this line in your crontab file:
10. Run a cron job every hour
To schedule a cron job to execute every hour at minute 0, you will need to add this line in your crontab file:
For instance, if the current time is 2 pm the job will run on 3:00 pm, 4:00 pm and so on.
11. Run a cron job after every 3 hrs
To schedule a cron job to run after every 3 hrs at minute 0, you will need to add this line in your crontab file:
For instance, if the current time is 2 pm the job will run on 5:00 pm, 8:00 pm and so on.
12. Run cron job at 4:00 pm on the first of every month
To schedule a job to run the day first of every month at 4:00 pm, you will need to add this line in your crontab file:
Strings in Crontab
You can also use the following strings rather than the long repetitive commands.
@midnight- Run cron job every day i.e. “0 0 * * *“
@daily- same as midnight
@weekly- Run cron job every week, i.e. “0 0 * * 0“
@monthly- Run cron job every month i.e. “0 0 1 * *“
@annually- Run cron job every year i.e. “0 0 1 1 *“
@yearly- same as @annually
@reboot- Run cron job at every boot
For instance, to run the cron job every day in the first minute and the first hour, add the following line:
For instance, to run the cron job each time the server is rebooted:
That is all there is to it! In this article, we have learned how to automate various tasks using crontab in the Debian system. By using the above examples, you can schedule any kind of job to initiate without any user interaction.