With the advent of new technologies, we want everything to be automated including our computer systems. For instance, as a system administrator, we may want our backup to be run at every night 2:00 am. Then what’s the purpose of computers if we have to wake up daily at 2:00 am and run the commands manually to initiate the backup. There should be a way that tells the system to automatically run a backup at a specific time. Luckily, Linux OS offers a cron utility that allows automating tasks at a specific period.

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:

* * * * /path/to/script.sh

Each of the fields corresponds to the following:

Minute (059) Hour (024) Day_of_the_Month (17) Month_of_the_Year (112)


Day_of_the_Week  (06) 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:

$ sudo crontab –u user -l

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:

Crontab in Linux crontab

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:

0 2 * * * /Documents/backup.sh

Crontab in Linux crontab

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:

* * * * * /path/to/script.sh

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.

*/10 * * * * /path/to/script.sh

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:

* * * jan,may,aug * /path/to/script.sh

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.

0 17 * * sun,fri /path/to/script.sh

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.

0 3 * * * /path/to/script.sh

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.

0 0 * * SUN /path/to/script.sh

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.

* * * * * /path/to/script1.sh; /path/to/script2.sh

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.

0 0 * * 15 /path/to/script.sh

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:

0 6,15 * * * /path/to/script.sh

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:

0 * * * * /path/to/script.sh

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:

0 */3 * * * /path/to/script.sh

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:

0 16 1 * * /path/to/script.sh

Strings in Crontab

You can also use the following strings rather than the long repetitive commands.

@hourly- Run cron job every hour i.e. “0 * * * *

@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:

@daily /path/to/script.sh

For instance, to run the cron job each time the server is rebooted:

@reboot /path/to/script.sh

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.

About the author

Crontab in Linux crontab

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.