Configuring Crontab In Ubuntu Explained

You can automate many tasks in your system or your server using Crontab in Ubuntu in order to prevent manual work. In this article, I will discuss configuring crontab in Ubuntu with examples.

What is Crontab?

The CRON daemon is an always running utility which executes certain commands at specific date and time. For programs that need to be executed recurringly, crontab file can be used. Crontab is a file that gives the CRON daemon shell commands to run at specified intervals.

Where Can You Use Crontab?

  • For sending periodic notifications about new features to your users.
  • To perform simple tasks like taking a backup of your database.
  • To perform cleanup activity on your log files so as to prevent storage issues.
  • For fetching the latest news from a website and display it on your system.

Installing Crontab

Before configuring crontab in Ubuntu, install it using following the given steps. If you have already have Crontab installed then you may skip to the next section.

Step 1: Update System Software

First of all, update your system software by running the following command. You might require sudo access for running these commands. If you do not have admin access, kindly contact your system administrator.

apt-get update && apt-get upgrade
Step 2: Verify if CRON Package is Installed

After updating check if CRON package is installed in your system.

dpkg -l cron
Step 3: Install CRON Package

If it is not installed then you can install the package with the following program:

apt-get install cron
Step 4: Edit Crontab

Type the following command in the terminal.

crontab -e

Your terminal should open the crontab file now. It may ask you to select an editor of your choice to open the file. The crontab file looks something like this:

Default Crontab File

Your terminal should open the crontab file now. It may ask you to select an editor of your choice to open the file. The crontab file looks something like this:

Crontab Commands

The following are the available Crontab commands along with their description:

CommandDescription
crontab -a filenameTo install filename as your crontab file.
crontab -eEdit or create a new crontab.
crontab -lDisplay crontab file.
crontab -rRemove crontab file.
crontab -vDisplay the last time crontab file was edited.

Understanding Crontab

Before configuring Crontab in Ubuntu, kindly get to know the fields provied by it. Every entry in the Crontab table consists of six fields:

minute(s) hour(s) day(s) month(s) weekday(s) command(s) 

The first five fields are integer fields containing the schedule for executing the statement. The sixth field is bourne shell command that need to be executed. The summary of all the fields is described below:

FieldValueDescription
minute0-59 The exact minute that the command executes.
hour0-23The hour of the day that the command sequence executes
day1-31The day of the month that the command sequence executes
month1-12The month of the year that the command sequence executes.
weekday0-6The day of the week that the command sequence executes (Sunday = 0, Monday = 1, Tuesday = 2, and so forth).
commandCommandThe Bourne Shell command to be executed.

You may also use an asterisk (*) in the first five fields to denote that all values are allowed. This will be further explained in the section below.

Examples

Example 1

Suppose we want to run a command every day at 2:15 PM. This is what our crontab will look like:

15 14 * * * /path/to/script/script.sh
Example 2

If we wanted to run this script only on Sunday at 2:15PM, then we can modify the crontab as follows:

15 14 * * 0 /path/to/script/script.sh
Example 3

For running the command on Sunday and Wednesday at 2:15 PM:

15 14 * * 0,3 /path/to/script/script.sh
Example 4

We can use hyphen for providing a range. The below command will be executed on Sunday, Monday and Tuesday at 2:15 PM:

15 14 * * 0-2 /path/to/script/script.sh
Example 5

For running a PHP script on 1st of every month at 12:00 AM:

0 0 1 * * php /path/to/script/script.php
Example 6

The following Python script will run thrice on every Wednesday and Saturday 4 AM, 10 AM and 2 PM.

0 4,10,16 * * 3,6 python path/to/script/script.py

Conclusion

In this article, you learned to configure Crontab in Ubuntu. Also, some examples of basic Crontab syntax were explained. You may also visit this link to check your Crontab syntax. Learn more about Linux on Concatly!

Kindly provide your valuable comments to help me improve my articles.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *