How to Automate and Schedule Tasks with Crontab on Ubuntu 16.04


  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
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)

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 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

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

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

The below values represent the months of the year:

37 8 21 7 * root wget

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 5 * * 3 root wget

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.

1,17,59 * * * * root wget
0 4,6,9 * * * root wget
0 0 1,16 * * root wget
0 0 1 1,4,7,10 * root wget
0 4 * * 0,3 root wget

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

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>
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:
* * * * * root wget >/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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: