Crontab is an important Linux tool that is used to schedule tasks so that programs and scripts can be executed at a specific time. In this article, I am going to teach you how you can schedule a job in Debian 10 and show you some examples.
You need to have root privileges before you proceed further.
Installing Cron and Crontab
In my Debian 10, cron is installed by default. However, if it is not installed on your machine, run the following few commands on the terminal with root privileges.
apt-get install cron
To get a list of cron jobs already scheduled on your machine, execute the following on terminal.
Opening crontab with a text editor
To open crontab with a text editor, execute the following command with root privileges.
As soon as the command is executed, you will be asked to choose the text editor as shown in the following screenshot.
The crontab -e command opens the crontab of the currently logged-in user, which is the root user in my example. To open the crontab of another user, let’s say the user ‘tom’, add the -u flag followed by the name of the user. Example:
crontab -e -u tom
Syntax of the crontab
Linux crontab has six fields as shown below.
* * * * * /path/to/script.sh
Each of the fields has the following meaning.
[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
Minute 0 – 59
Hour 0 – 23
Day of month 1 – 31
Month of year 1 – 12
Day of week 0 – 7
A basic example of a cron job
Here are some of the examples of cron jobs.
1. Schedule a cron job to execute at 2 AM daily
0 2 * * * /bin/sh backup.sh
The above cron job will be executed daily at 2 AM and will run a script backup.sh. This will maintain the backup every day.
2. Schedule a cron job to execute twice a day
0 5,17 * * * /scripts/script.sh
Above cron job will be executed at 5 AM and 5 PM daily. Multiple times can be specified with the help of comma.
3. Schedule a cron job to execute on every minute
* * * * * scripts/script.sh
The above cron job will be executed on every minute.
4. Schedule a cron job to execute on every Sunday at 5 PM
0 17 * * sun /scripts/script.sh
The above cron job will be executed on every Sunday at 5 PM. This type of cron is useful for doing weekly tasks like log rotation etc.
5. Schedule a cron job to execute on every 10 minutes
If you would like your job to execute every 10 minutes, cron needs to be updated as follows.
*/10 * * * * /scripts/monitor.sh
‘*/10’ means to run every 10 minutes.
6. Schedule a cron job to execute on selected months
Suppose you want to execute a cron in January, May, and August, the cron job needs to be set up as follows.
* * * jan,may,aug * /script/script.sh
Again multiple months can be specified by a comma.
7. Schedule a cron job to execute on selected days
If you would like your cron job to be executed on selected days suppose Sunday and Friday at 5 PM, it should look like the following.
0 17 * * sun,fri /script/script.sh
8. Schedule multiple tasks in a single cron job
Multiple scripts can be run in a single task as follows. Both the scripts should be separated by a semicolon.
* * * * * /scripts/script.sh; /scripts/scrit2.sh
9. Schedule a cron job to run every 30 seconds
To schedule a cron job to execute every 30 seconds, we need to set up two cron’s as follows:
* * * * * /scripts/script.sh * * * * * sleep 30; /scripts/script.sh
10. Schedule a cron job to execute twice on every Sunday and Monday
If you would like to schedule a job to execute twice at 4 AM and 5 PM every Sunday and Monday, the cronjob should look like this:
0 4,17 * * sun,mon /scripts/script.sh
Both, the hour and week of the day have been separated by commas.
Thank you for reading this article on cron jobs. Have a great day!!