How To Schedule Tasks Or Cron Jobs Using Crontab On Ubuntu

How To Schedule Tasks Or Cron Jobs Using Crontab On Ubuntu

Explains how to schedule tasks also known as cron jobs in Ubuntu using crontab. Crontab can be used to schedule any command or executable program in order to automate it.

December 15, 2019

Sometimes we need to execute commands or programs automatically at a fixed time. The scheduled jobs or tasks can be executed either once or daily at fixed hours. We can also plan to automate the tasks to run on a specific day of a week or month. These scheduled jobs or tasks are also called as cron jobs. We can use Crontab in Ubuntu to schedule the tasks. We can automate and schedule regular backups of the key resources or project files without any human intervention.

Verify Cron

In the very first step, we will verify whether the cron package is installed on the system. It can be done using the command as shown below:

# Update System

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get autoclean
sudo apt-get --purge autoremove

# Verify Cron
dpkg -l cron

# Output
+++-==================-==============-==============-=========================================
ii cron 3.0pl1-128.1ub amd64 process scheduling daemon

The command dpkg -l cron shows that the cron is already installed on the system.

You can install the cron package in case it's not installed on your system using the command as shown below.

# Install Cron
sudo apt-get install cron

After confirming that cron package is installed on the system, we can also check it's status usig the command as shown below. The output is shown in Fig 1.

# Cron Status
sudo systemctl status cron
# OR
sudo service cron status

Cron Status in Ubuntu

Fig 1

Getting Familiar With Crontab

After confirming that cron package is installed and active on the system, let's get familiar with it. We will start with creating or editing the file. The same command can be used to create or modify the crontab file as shown below.

# Create/Update Crontab File
crontab -e

Now choose the editor of your preference. I have selected the easiest one i.e. nano to create/edit the file. It will create the file in case it does not exist. The editor options and the default instructions in the file are shown in Fig 2 and Fig 2.

Cron Editor Options

Fig 2

Cron Instructions

Fig 3

It shows the crontab syntax as shown below. We can specify each schedule task or job on it's own line following this syntax.

# Crontab Syntax
m h dom mon dow command

We can either specify the concrete value for these options or an asterisk i.e. * for any value.

m - The first option m stands for minutes. It can take values from 0 to 59 or * for every minute.

h - The second options is to specify hours. We can assign the values from 0 to 23 or * for every hour.

dom - Day of Month - We can assign the values from 1 to 31 or * for every day of the month.

mon - It specifies the month. We can assign the values from 1 to 12 or * for each month.

dow - Day of Week - We can assign the values from 0 to 6 or * for each day of the week.

command - The command can be either system command or an executable program or an script having batch of jobs.

Examples

This section shows the examples to have an entry on the crontab file.

# Once in every hour
0    *    *    *    *    <command>

# At every 15th minute
*/15    *    *    *    *    <command>

# Every day at 0th minute, 0th hour
0    0    *    *    *    <command>

# Every day at 30th minute, 5th hour
30    5    *    *    *    <command>

# Twice a day

0    0    *    *    *    <command>
0  12   *    *    *    <command>

# At 0 minute, past every 12th hour
0  */12   *    *    *    <command>

# On first day of every month
0    0    1    *    *    <command>

These are some of the basic examples of the crontab to run the command or execute the file at specific time. We can specify the command to be run either every our, every day, or every month as shown above. Make sure to replace the <command> with your command or path to an executable file as shown below.

# On first day of every month
0    0    1    *    *    /data/jobs/newsletter.sh

In case your file is not executable, you can do so as shown below.

# Executable script
sudo chmod +x /data/jobs/newsletter.sh

Summary

This tutorial provided the instructions to verify whether cron package in installed on the system and to update the crontab file to schedule jobs and tasks.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS