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.
- 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.
- An Ubuntu 18.04 VPS plan
- A non-root user with sudo privileges
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
m h dom mon dow user header has the following meaning
- m – The minute when the cron job will run from 0-59
- h – a numeric value determining the hour when the tasks will run acceptable values run from 0-23
- dom – Day of the Month when the cron job will run from 1-31.
- mon – The month when the cron job will run from 1-12
- dow – Day Of the Week from 0-6 with Sunday being 0
- user – The User under which the cron will run
- 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.
* * * * * 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.
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.