How to set up a cron job in Debian 10

Crontab is an important Linux tool that is used to schedule tasks so that programs and scripts can run at specific times. In this article, I will tell you how you can plan your work in Debian 10, and show you some examples.

Background

You must have root privileges before proceeding.

Install Cron and Crontab

In my Debian 10 cron is installed by default. However, if it is not installed on your computer, run the following few commands in a terminal with root privileges.

apt-get update
apt-get install cron

To get a list of cron jobs already scheduled on your computer, follow these steps on the terminal.

crontab -l

Opening crontab with a text editor

To open crontab in a text editor, run the following command with root privileges.

crontab -e

Once the command is completed, you will be prompted to select a text editor, as shown in the following screenshot.

The crontab -e command opens crontab for the currently logged in user, who in my example is the root user. To open crontab to another user, say user “tom,” add the -u flag and then the username. Example:

crontab -e -u tom

Crontab syntax

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]

Minutes 0 – 59

Hour 0 – 23

Day of the month 1 – 31

Month 1 – 12

Day of the week 0 – 7

Basic cron example

Here are some examples of how cron works.

1. Schedule a cron job at 2 a.m. daily.

0 2 * * * /bin/sh backup.sh

The above cron job will run daily at 2am and will run the backup.sh script. This will maintain a backup every day.

2. Schedule a cron job twice a day.

0 5,17 * * * /scripts/script.sh

The task above cron will be performed at 5 a.m. and 5 p.m. daily. You can specify it several times with a comma.

3. Schedule a cron task for every minute.

* * * * * scripts/script.sh

The above cron job will run every minute.

4. Schedule a cron job every Sunday at 5 p.m.

0 17 * * sun /scripts/script.sh

The above cron work will be done every Sunday at 17:00. This type of cron is useful for performing weekly tasks such as rotation of magazines, etc.

5. Schedule a cron job every 10 minutes.

If you want your work to be done every 10 minutes, you need to update cron as follows.

*/10 * * * * /scripts/monitor.sh

“* / 10” means run every 10 minutes.

6. Schedule the cron job for the selected months.

Suppose you want to run cron in January, May, and August, the cron job needs to be configured as follows.

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

Again, a few months can be indicated with a comma.

7. Schedule the cron job for the selected days.

If you want your cron work to be done on certain days, for example, Sunday and Friday at 17:00, it should look like this.

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

8. Schedule multiple tasks in one cron job

Several scripts can be run in the same task as follows. Both scripts must 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 task to run every 30 seconds, we need to configure two cron as follows:

* * * * * /scripts/script.sh * * * * * sleep 30; /scripts/script.sh

10. Schedule a cron job twice every Sunday and Monday.

If you want to schedule a task to run twice at 4:00 and 17:00 every Sunday and Monday, the cronjob should look like this:

0 4,17 * * sun,mon /scripts/script.sh

The hour and week of the day are separated by commas.

Conclusion

Thanks for reading this article about cron. Have a nice day!!

How to set up a cron job in Debian 10

Sidebar