How to replace WordPress cron with a cron job in cpanel Print


Have you sometimes noticed that your WordPress installation take a few extra seconds to load? Or maybe that your scheduled posts fail to publish on time? This maybe due to your the WordPress cron function.

What is a Cron Job?

As stated on Wikipedia (http://en.wikipedia.org/wiki/Cron) The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

In layman terms this is a program you can access through your cPanel hosting control panel to ensure that another program does the same thing at a given time period.

What is a WordPress Cron?

WordPress comes with it's own Cron which enables you to schedule posts or automate events with WordPress. This is part of the core features and is triggered when a visitor browses to your website and navigates through the pages.

So if WordPress has it's own cron where is the problem? Well, there are numerous issues that can arise for why the process doesn't complete.

This can range from:

  • Your site doesn't receive visitors when needed
  • DNS mis-configuration
  • Server Overloaded
  • Caching plugins stopping wp-cron.php from firing.
  • and the list goes on

This means that if WordPress cron doesn't get triggered at the right time, the post doesn't schedule. Also as this is adding an additional program to run when your site is visited this can add to the load time as WordPress needs to go through an automation tasks before it can get to rendering the page for you.

How to replace WordPress Cron (wp-cron.php) with an actual Cron Job?

Once logged into your web hosting control panel, navigate to the "Advanced" section at the bottom or cPanel and click on Cron Jobs.


Within the Cron Job panel you will be able to add a new task or view/amend existing tasks.

Adding a new task

The settings available to trigger a task are:

* * * * * command to execute
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └─────────────────── month (1 - 12)
│ │ └───────────────────── day of month (1 - 31)
│ └─────────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

Note: In Cron Jobs "*" means "every"

When adding a new task you can select pre-defined time schedules from the first drop-down list, but we need something slightly different.

We need the process to trigger every 15 minutes which needs us to select "Once per 5 Minutes (*/5)" from the second drop down list, then in each of the other boxes we just need * (Every).

Using WGET:

Using the "wget" command, which is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive command-line tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.

The code that we need to execute is:

wget -q -O - http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

You will need to change example.com to the specific location to your WordPress installation.

Using WP-CLI (Recommended):

As we also have WP-CLI installed on our cPanel servers, we can also use this to trigger the scheduled tasks. WP-CLI is again a command-line program but it is purely designed for interacting with your WordPress website within having to log into your dashboard.

The code that we need to execute is:

/usr/local/bin/php /usr/local/bin/wp cron event run --due-now --path=/home/USER/public_html/ >/dev/null 2>&1

To break this down:

/usr/local/bin/php is telling the script to use the PHP binary to execute the command. You can confirm this is correct for you with the command "whereis php" from your terminal

/usr/local/bin/wp is telling the PHP to use the WP-CLI binary program. You can confirm this is correct for you with the command "whereis wp" from your terminal

cron event run --due-now will command WP-CLI to execute any tasks that are currently due.

--path=/home/USER/public_html/ As WP-CLI will execute in the directory that it is run from, we need to specify exactly where WordPress is installed. You will need to customise this for your installation. You can confirm this by navigation to your WordPress directory then using "PWD" (Print working directory) from your terminal.

>/dev/null 2>&1 this final part tells the full command to send any standard or error output to /dev/null so that you logs don't get filled with erroneous data.

Stopping wp-cron.php

Now that we have set-up the server to trigger your site, we need to stop WordPress triggering it each time a visors browses to your site.

Within the web hosting control panel scroll to the top of the page and click "Home", the navigate to the "File Manager" from the top "Files" section.

This gives the options to navigate to different parts of your web hosting account. For our purpose we need to select "Web Root", which will open in a new tab.

Within the new tab the page splits into three sections:

  • Top: File Operation
  • Left: Folder navigation
  • Right: Files within the selected folder

Note:If you have installed WordPress to a sub-folder i.e. example.com/wordpress you will need to navigate to this folder in the left hand section

Once you have found the root folder of your WordPress installation you need to single click to select "wp-config.php" in the right hand section.

Next select "Code Editor" from the Top file operations section; Again this will open in a new tab.

The file that you have just opened contains some crucial settings for your WordPress to work so make sure that you don't change anything!

We need to navigate to the bottom of the file and add:

define('DISABLE_WP_CRON', true);

You're done!

How to test

Set-up a new test post that is scheduled for 20 mins in the future.....Wait...... If WordPress has automatically published the post then everything has worked as planned. If not, run through the tutorial again, or log a support ticket and we will look into it for you.


Was this answer helpful?

« Back

Find your perfect domain

Use our domain name search to check your dream domain name

Wide Variety of Domains

Easy Registration Process

Free WHOIS Privacy

www.
Spinner