×
Community Blog How to Automate and Schedule Tasks with Crontab on Ubuntu 16.04

How to Automate and Schedule Tasks with Crontab on Ubuntu 16.04

In this article, we will walk you through the whole process of automating and scheduling tasks with crontab on an Alibaba Cloud ECS instance.

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Cron is a Linux based utility for scheduling time-based jobs that run automatically at a set time, date or after a specific interval. You can automate various repetitive administrative tasks (e.g. database backups, email reminders, etc) using cron jobs.

The beauty of cron job lies on accuracy. Since the overall process of running the tasks is automated, you can use this utility to execute mission critical instructions without ever forgetting.

You can use crontab to automate and schedule tasks on your Linux machine. This is simply a file-based configuration table with lists of jobs and timings when tasks are scheduled to run.

This step-by-step guide will walk you through the whole process of automating and scheduling tasks with crontab on an Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 16.04 instance.

Alibaba Cloud ECS is a secure, affordable and world-class cloud computing service that offers Virtual Private Servers (VPS). You can use Alibaba Cloud enterprise-oriented computing instances to cater for all your hosting needs including running web servers, database servers, email servers and web-based applications.

Prerequisites

  1. A valid Alibaba Cloud Account (Sign up now and get up to $1200 to test over 40 Alibaba Cloud products).
  2. An Alibaba Cloud ECS instance running Ubuntu 16.04 Operating System.
  3. A non-root user with sudo privileges.

Step 1: SSH To Your Alibaba Cloud ECS Instance

Identify the public IP address associated with your Alibaba Cloud ECS instance. Then, using an SSH client, connect to your server. If you are running Windows on your local computer, you can use PuTTY. However, Linux and macOs have built-in command line interfaces that you can use without downloading third party applications.

Step 2: Configuring the Crontab File

Next, we are going to open the crontab configuration file using nano for editing purposes. The file is located at /etc/crontab. Run the command below to open it:

$ sudo nano /etc/crontab

The basic syntax for writing cron jobs is listed towards the end of the file with some few scheduled tasks to get you started.

First, Let's go over the structure and arrangement of jobs before we start creating cron jobs.


m    h   dom   mon dow user command
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

m: This stands for minute. You can specify a number between 0 and 59 to define the exact minute when the scheduled task will run.

h: This stands for hour. Depending on what you want to automate, you can use a value between 0 and 23. It is advisable to run a cron job when the system is not busy. For instance, a database backup can be ideally run at the middle of the night by specifying 0 as the value.

dom: The third value represents the day of the month from 1-31. Use of this value is very practical in mission critical applications. For instance, if you have a billing system, you can use the day of the month value to trigger a script that sends invoices and subscription emails to your customers on the 1st day of each month by specifying 1 here.

mon: You can explicitly instruct cron to run only on specific months. The accepted range is 1-12. For instance, if your clients are paying a yearly charge that falls on January for your services, you can indicate 1 here. Likewise, you can use 1, 4,7,10 to run the task on a quarterly basis.

dow: In a production environment, there are number of scenarios that may require a cron job to run at specific days of the week. For instance, consider a situation where a bank applies penalties to its clients on Monday for those members with weekly loan arrears. They can use a value of 1 to run those penalties on that specific day.

user: You can specify the user under which the command will run on this location. Sometimes, you may use root for administrative tasks that require elevated privileges

command: Once you have specified the timings, you also have to set the command that will be run at the specified time. The options here are unlimited. For instance, you can schedule tasks to download a file using wget command, run a PHP or Perl Script, trigger a database backup or even initiate copying of files using the cp command.

Step 3: Examples of Real-Life Cron Jobs on Ubuntu 16.04

With our well-thought and complete explanation of how cron job syntax is written above, let us create and explain some cron jobs that you can use in real-life to prove the concept.

A Cron Job for Downloading a File at Midnight

The cron job below will download a file from example.com website at the middle of the night exactly at 00:00 hrs. Please note we have used zeros for the first 2 parts. The wildcard characters (*) on the other parts instruct the cron to run every month, day or weekday. This means the cron will run daily at midnight.

0 0 * * * root wget http://www.example.com

A Cron Job That Runs Every Minute

Sometimes, you might feel the need to create a cron job that runs every minute. You can use the syntax below. However, use this sparingly as it may haunt your server resources if the command involves long running tasks.

* * * * * root wget http://www.example.com

A Cron Job That Runs Once a Year at July 21st at 8:37 AM

The below values represent the months of the year:

1: January

2: February

3: March

4: April

5: May

6: June

7: July

8: August

9: September

10: October

11: November

12: December

The below cron will run once a year on the 21st day of July (7th Month) specifically at 8:37 AM:

37 8 21 7 * root wget http://www.example.com

We have specified * on the dow part to let the cron run irrespective of the day of the week when our date falls on.

A Cron Job That Runs Each Week on Wednesday at 5:00 AM

The days of the week are represented with the below values;

0 - Sunday

1 - Monday

2 - Tuesday

3 - Wednesday

4 -Thursday

5 - Friday

6 - Saturday

So to schedule a cron job to run on Wednesday, we will use a value of 3 on the dow(day of the week) part. Also, we will use 5 on the h(hour) part to signify 5 PM and 0 on the minute part to signify top of the hour. We can use * on the other values to let the cron run irrespective of the date or month of the year.

0 5 * * 3 root wget http://www.example.com

Step 4: Setting Multiple Values on Cron Jobs

Although you can set multiple cron jobs to run at different timings, you can make the configuration file cleaner by setting multiple timings on one line by separating values with commas.

For instance, if you want a cron job to run on the 1st, 17th and 59th minute, you can use the syntax below:

1,17,59 * * * * root wget http://www.example.com

Likewise, to run the cron at 4, 6 and 9 AM use the syntax below

0 4,6,9 * * * root wget http://www.example.com

To run a task on 1st and 16th day of the month at midnight, use the below syntax

0 0 1,16 * * root wget http://www.example.com

To run a task on January, April, July and October (Quarterly) at midnight on the 1st day of the month, use the syntax below:

0 0 1 1,4,7,10 * root wget http://www.example.com

To run a task on Wednesdays and Sundays at 4:00 AM, use the syntax below:

0 4 * * 0,3 root wget http://www.example.com

Step 5: Setting Up a Cron Job to Run After a Specific Time Interval

You can use the modulus operator(/) to run a cron job after every specific time interval. For instance, to run a cron job after every 15 minutes, use the syntax below:

*/15 * * * * root wget http://www.example.com

This means the cron will run on the 0th, 15th, 30th and 45th minute. That is, 4 times every hour.

Step 6: Executing PHP Scripts Using Cron Jobs

You can execute a PHP script using cron job by using the below syntax:

m h dom mon dow user /usr/bin/php -q <PHP_file_to_execute>

For instance, the cron job below will trigger a PHP script that creates automatic MySQL backups for a server each day at midnight:

0 0 * * * root  /usr/bin/php -q /var/backups.php

Step 7: Setting Email Notifications When a Cron Job Is Executed

For testing purposes, you can configure crontab to send an email every time a cron job is executed. To do this, include the line below at the top of your /etc/crontab file:

MAILTO=info@example.com

Remember to replace info@example.com with the correct email address where you would like to receive the emails.

By default, you will receive an email every time a cron job is executed. If you think this is not desirable for some of the tasks that you have scheduled, redirect their output to >/dev/null 2>&1 as shown below:

* * * * * root wget http://www.example.com >/dev/null 2>&1

Conclusion

In this article, we have gone through the process of automating and scheduling tasks with crontab on Ubuntu 16.04 server hosted on Alibaba Cloud ECS. We have covered the general syntax of adding jobs on crontab configuration file and ran a few real-life examples.

We believe that you will use the knowledge learnt in this article to simplify repetitive administration tasks on your Linux server. If you are new to Alibaba Cloud, you can sign up and get up to $1200 to test over 40 Alibaba cloud products.

1 1 1
Share on

francisndungu

31 posts | 8 followers

You may also like

Comments

Raja_KT March 10, 2019 at 1:02 pm

Good one. It seems to be easy for Techies. But at the time of coding, this is quite fashionably useful referring, just copy and paste and make changes :)

francisndungu

31 posts | 8 followers

Related Products