How to Automate and Schedule Tasks With Crontab on Ubuntu 16.04
How to Automate and Schedule Tasks With Crontab on Ubuntu 16.04
Cron jobs are great for removing monotony from your workflow. Read on to lear how to use them in an Ubuntu-based dev environment!
Join the DZone community and get the full member experience.Join For Free
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 jobs 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.
- A valid Alibaba Cloud Account (Sign up now and get up to $1200 to test over 40 Alibaba Cloud products).
- An Alibaba Cloud ECS instance running Ubuntu 16.04 Operating System.
- 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 first day of each month by specifying
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 the
wget command, run a PHP or Perl Script, trigger a database backup, or even initiate copying of files using the
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, 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 (
0) for the first two 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 on July 21 at 8:37 AM
The below values represent the months of the year:
The below cron will run once a year on the twenty-first day of July (the seventg 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 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
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) parts to signify 5 PM and
0 on the minute part to signify the 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 times, 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 first, seventeenth, and fifty-nineth minute, you can use the syntax below:
1,17,59 * * * * root wget http://www.example.com
Likewise, to run the cron at 4:00AM, 6:00AM, and 9:00 AM use the syntax below:
0 4,6,9 * * * root wget http://www.example.com
To run a task on the first and sixteenth day of the month at midnight, use the below syntax:
0 0 1,16 * * root wget http://www.example.com
To run a task in January, April, July, and October (quarterly) at midnight on the first 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
45 minute markers. That is, four times every hour.
Step 6: Executing PHP Scripts Using Cron Jobs
You can execute a PHP script using cron jobs 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:
Remember to replace email@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
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.
Published at DZone with permission of Francis Ndungu , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.