What is a cron job?

A cron job is a repetitive task on a web server that runs as a shell script or command line function on the basis of a timing pattern established by administrators. On a technical level, a cron job operates on Linux web servers as directed by the settings in the crontab or cron table file at fixed intervals of time. 

A cron job executes a script that runs various commands on a web server as required by the software being hosted. A cron job is used by CMS platforms like WordPress and Drupal in web publishing for scheduling automated tasks related to module functionality and maintenance, requiring web server configuration. 

WP-Cron is used by the WordPress CMS for scheduling jobs related to plugins and themes. A cron job instructs plugin and module elements to update their settings, download content from APIs, clear caches, or perform other tasks related to automation. You can schedule cron jobs with cPanel, Plesk, and other admin tools. 

Cron job definition 

A cron job is an automated task on a Linux-based web server that is run by the command line at set intervals of time repeatedly. Cron is the software component on the web server that allows the job to be scheduled. The timing pattern for the script is established by the crontab or cron table which establishes a repeating schedule for the script to run. A cron job is important in CMS website publishing. 

In Linux terminology, cron operates as a daemon or a background process that executes non-interactive jobs on a web server. The daemon operates a command-line shell script on the schedule dictated by the crontab. The crontab file is usually located at /etc/crontab on the web server with further automation by subfolders.  

Administrators can set up a sub-file system in the crontab folder like: 

  • /etc/cron.hourly/ – To run all scripts once an hour. 
  • /etc/cron.daily/ – To run required scripts once a day. 
  • /etc/cron.weekly/ – To run scheduled commands once a week. 
  • /etc/cron.monthly/ – To run all operations once a month. 

Placing cron jobs in these folders will automatically schedule them to run at the respective intervals. Otherwise, it is required to use a control panel utility like cPanel, Plesk, etc., to set up a custom cron job for script automation and CMS maintenance. Experienced systems administrators can also schedule cron jobs using the command line to create and manage crontab files on the web server. 

Cron job usage 

A cron job is used by CMS scripts like Drupal for indexing website content and checking the home server for platform updates. WordPress uses WP-Cron to check for updates and publish scheduled posts. Both CMS platforms are extensible so that plugins, modules, and themes can include custom functionality that runs with every cron job. All of these rely on the Linux cron function to run. 

Web publishers have the option to schedule cron jobs on their web server or to use a plugin/module solution that runs the operations from a PHP script. Although these are functionally equivalent, the PHP solutions override the crontab functionality on the web server, removing the need to configure cron jobs as command-line scripts in order to support shared hardware environments. 

The scheduling of cron jobs through cPanel, Plesk, and other control panel utilities is relatively simple. There are settings with granularity to the month, day, hour, and minute that can be scheduled with a particular command. You can schedule cron jobs for different scripts in support of multiple domain names. It is required to enter the command to be run by the cron job in the settings with the timing. 

Basic script examples 

If you are using the command line to manage cron jobs, some of the important commands to know are:

crontab -l

This command will list the contents of the current crontab. 

crontab -e

This command will allow you to edit or create a new crontab.

crontab -r

This command will remove the current crontab file from the server.

crontab -u [username]

This command will reference only files related to a particular user.

crontab -v

This command will allow you to view the most recent changes to the crontab.

When adding a crontab, the time settings must follow the pattern of

minute(s) hour(s) day(s) month(s) weekday(s) command(s)

When this is combined with the command to be run, the crontab will display as:

* * * * * command(s)

 

If you do not have experience using the command line to manage a web server or are using a shared hosting plan with cPanel, you can reference the following:

cron job settings

Click on the “Cron Jobs” icon under the “Advanced” tab in cPanel to enter the configuration section. There you will see a table with settings for the weekday, month, day, hour, and minute. A cron job schedule cannot be set with more frequency than one minute on a web server. Use numerical values for the time in each section or enter an asterisk for automation every day, month, and year. 

Enter the script command required by your CMS platform or software and save the cron tab settings in cPanel. The command will then run automatically on the server at fixed intervals according to the time settings that were established. You can reference the official tutorial by cPanel for more information on the process.

Cron job usage on the web 

If you are using the Drupal CMS for web publishing, the core functionality includes crontab scheduling without the need to set it on the web server. However, it is recommended to configure the server directly for more reliable results. The basic command required by the Drupal CMS for cron job scheduling is:

0 * * * * wget -O - -q -t 1 http://CRON_URL

In this instance, there are different settings for the URL to be called in the command depending on the version of Drupal being used:

 

  • Drupal 8:
    http://www.example.com/cron/<key> 

  • Drupal 7:
    http://www.example.com/cron.php?cron_key=<key> 

  • Drupal 6:
    http://www.example.com/cron.php 

 

Scheduling the cron job to a particular time interval will run the file listed in the URL settings automatically, allowing the CMS to perform regular maintenance. 

If you need to set up a cron job for WHMCS, navigate to the “Automation Status” in the admin section. There you should see a link to “Cron Status” which will give you the URL of the command that needs to be automated for WHMCS to run. 

WHMCS recommends setting the cron job to run every five minutes on the web server. In order to do this, copy the URL command from the “Automation Status” and enter it in cPanel with settings for the timing pattern:

cron command

The cron command URL is unique to your WHMCS installation on the web server. After the cron job is configured, the CMS will function regularly with automation. 

Using cron with WordPress 

As part of the core CMS functionality, WordPress is configured to run wp-cron.php every time a page is loaded on a website. While this is useful to keep the content up to date, it is potentially resource-intensive and furthermore less reliable for low-traffic WordPress websites than scheduling a regular cron job. 

In order to turn off the automatic functionality of wp-cron.php, it is required to edit the wp-config.php file for WordPress by adding the line:

define('DISABLE_WP_CRON', true);


After this change is saved to the web server, you can log in to cPanel to schedule a cron job using the web server’s functionality. Set the cron job to run every 30 minutes with the command:

cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php

What is WP-Cron?

WordPress allows plugins and themes to build on the functionality of wp-cron.php to schedule automated tasks with cron jobs. This is accomplished using the wp_schedule_event() function in PHP. The wp_schedule_event() function has three main variables: $timestamp, $recurrence, and $hook. These variables establish when the cron script will run, repeat, and the code to be utilized. 

While it is recommended to replace the default settings for wp-cron.php by running it directly with a scheduled cron job on the web server, there are also other plugins that will run the crontab from PHP code. WP Control is the most popular cron plugin for WordPress with the ability to expand wp-cron.php administration and functionality. Easycron and Simple Cron are other scheduling solutions for WordPress that install as plugins for optimized CMS administration.