How to Set Up Cron Jobs on Your Ubuntu 18.04 Dedicated Server or VPS

How to Set Up Cron Jobs on Your Ubuntu 18.04 Dedicated Server or VPS

Cron jobs are essential when it comes to scheduling automated tasks on your Ubuntu 18.04 server. Most webmasters use them to run scripts automatically.

In Linux, a ‘cron’ is a time-based job scheduler while a ‘cron job’ is a task. Ubuntu 18.04 has a daemon that runs in the background and is responsible for initiating cron jobs on the system. The Linux scheduler processes tasks that reside in a configuration file known as crontab.

So, all tasks related to cron jobs are saved on the configuration files together with their timings.

Possible use of cron jobs

    • Generating and sending invoices to clients via emails automatically for subscription services.

<li”>Deactivating customer accounts that stay inactive for a long time.

  • Running an email API (Application Programming Interface) to send newsletters to subscribers.
  • Applying penalties to loan defaulters if the loan payment date is not honored.

Setting up a cron job

A simple cron job syntax display’s the timing, user, and command to run. Before we learn about the cron job syntax, make sure you have everything listed in the prerequisites section below.

Prerequisites

  • An Ubuntu 18.04 VPS plan
  • A non-root user with sudo privileges

Special Note: Consult with HostAdvice’s Best Linux Hosting Services page or Best VPS Hosting page to find the top Linux VPS hosting services.

Step 1: Open crontab using the nano text editor

>In order to edit the configuration file where all cron jobs reside, run the command below on your terminal window:

$ sudo nano /etc/crontab

Once you open the file for editing, you will see the following header and a list of sample cron jobs

The m h dom mon dow user header has the following meaning

  1. m – The minute when the cron job will run from 0-59
  2. h – a numeric value determining the hour when the tasks will run acceptable values run from 0-23
  3. dom – Day of the Month when the cron job will run from 1-31.
  4. mon – The month when the cron job will run from 1-12
  5. dow – Day Of the Week from 0-6 with Sunday being 0
  6. user – The User under which the cron will run
  7. command- The linux command you wish to execute e.g. to run a backup using a PHP file you can use the command * * * * * root /usr/bin/ phpfile.php

Example of cron jobs

You can play around with the crontab timings to run your tasks at any time of the day throughout the year and here are some basic examples that might be helpful

A cron that runs every minute

* * * * * [user] [command]

A cron that runs on the 5th minute every hour

5 * * * * [user] [command]

A cron that runs every hour (every time the minute becomes zero)

0 * * * * [user] [command]

A cron that runs every day at midnight(every time the hour becomes zero)

0 0 * * * [user] [command]

A cron that runs every day at 5:15 am in the morning in the month of December only

15 5 * 12 * [user] [command]

Asterisks on the crontab timings

In most cases, you might use an asterisk instead of a number on the timings. The asterisk instructs the cron daemon to run the task on all possible values for that position. For instance, if you use an asterisk on the h part, the cron will run every hour.

Division Operator/forward slash/Mod Operator on Ubuntu 18.04 Cron jobs

You may use a forward slash to specify stepped values on the crontab for instance, the command below means that the cron job will run every 5 minutes. In other words, the mod sign tells the crontab daemon to run the command listed for every minute that is divisible by zero without a remainder e.g. 0,5,15,20,25,30,35,40,45,50,55

*/5 * * * * [user] [command]

Commas on the crontab

You may specify different values on a single position separated by commas to make a cron job run on several different timings. For instance, if you are designing an application that will apply a quarterly fee on a client account, you might use the following crontab syntax

0 2 1 1,4,7,10 * [user] [command]

The command above will run the cron job on the first day of January, April, July, and October at 2 am. Also, although it may sound weird to some webmasters, you might use words on the scheduling and it will still work.

0 2 1 Jan,Apr,Jul,Oct * [user] [command]

Dashes on the Ubuntu 18.04 crontab

Dashes indicate a range. For instance, a task can be run once every hour between 6 and 9 am using the syntax below.

0 6-9 * * *[user] [command]

Running PHP files on Ubuntu 18.04 crontab

While Ubuntu 18.04 server side CGI (Common Gateway Interface) scripts will run by default, PHP scripts will require some tweaking.

To run a PHP script on the crontab, use the below syntax:

* * * * * /usr/bin/php [path to your php script]

This will force the PHP file to run through the correct PHP interpreter referenced on the path e.g. /usr/bin/php.

Example

* * * * * root /usr/bin/php /var/backup.php

The code above will run a PHP script every minute to automate a PHP backup process on the server.

Conclusion

In addition to its fast boot time and major bug fixes, Ubuntu 18.04 has flexible tools when it comes to running your cron jobs. Remember to press CTRL+X and S when you finish editing the ‘/etc/crontab’ configuration file on the nano editor. Also, you should set your server to the appropriate time zone because cron jobs rely on your server’s date/time and this must be set correctly for accuracy purposes.

Check out the top 3 VPS hosting services:

A2 Hosting
$2.99 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
FastComet
$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
Hostwinds
$4.99 /mo
Starting price
Visit Hostwinds
Rating based on expert review
  • User Friendly
    4.8
  • Support
    4.0
  • Features
    4.8
  • Reliability
    4.3
  • Pricing
    3.5

How to Create a Non-root User on Your Ubuntu 18.04 VPS or Dedicated Server

This guide shows you how to create a user on your Ubuntu 18.04 virtual server.
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install a Self-Signed SSL Certificate on Your Ubuntu 18.04 VPS or Dedicated Server

This how-to article will teach you how to create a self-signed SSL certificate o
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install WordPress on Your Ubuntu 22.04 VPS or Dedicated Server

This tutorial will show you how to install WordPress on your Ubuntu 22.04 virtua
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Disable MySQL 5 “Strict Mode” on an Ubuntu 18.04 VPS or Dedicated Server

How to Disable MySQL Strict Mode on MySQL 5.6 Running on an Ubuntu 18.04 VPS.
2 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top