Replace WordPress Cron with Real Cron for Site Speed

How to replace your WordPress cron job with a server cron job to increase site speed.

We can all relate to a slow WordPress website with a very long request times. Most of us have also seen the WordPress “Missed Schedule” error message for content such as future scheduled posts that were supposed to be published but weren’t. The list goes on and the WordPress cron job is the cause.

S3JE5YAMND

Inexplicably slow WordPress sites with high CPU usage is frustrating. You need a break so let’s fix this!

 

How Does the WordPress Cron Work?

In reality, the WordPress cron is a system that has a set list of tasks, each with a time when it needs to be executed and it’s once or recurring status.

When the time comes – or has lapsed – the WordPress cron executes the task and continues with the list.

Visitor traffic is required for WordPress to keep track of time and scheduled tasks to execute with the cron.

 

Why Would the WordPress Cron Fail?

There are many reasons why a cron job could either fail or be delayed:

  • Server overloaded and as a result the cron cannot execute.
  • Not enough or not frequent enough visitors on the website.
  • A bug or conflict caused by a plugin or combination of plugins.
  • and so on…

 

Benefits of Using a Server Cron Job Instead

Replacing your WordPress cron job with a real, server cron job is beneficial in many ways:

  • Reliable, accurate cron jobs that fire on time as expected
  • Make your WordPress website faster to your users/visitors
  • Eliminate high CPU usage caused by WordPress

Both benefits are important but the 2nd one is especially important.

Unfortunately, when someone visits your website and the WordPress cron has a task to run at that moment, it will run that task on the visitor’s time, increasing the time of the request or even ending up giving that visitor a blank page if the task cannot complete.

So you take the cron jobs off your visitors and relaying it to the server side of things.

 

How to Replace the WordPress Cron?

Before attempting the below, please read the following article first to make sure that the issue isn’t caused by one of the points mentioned there: https://tribulant.com/docs/wordpress-mailing-list-plugin/11164/make-sure-the-wordpress-cron-works/

 

To replace the WordPress cron, it is quick and easy – let me show you how in 2 steps:

1. Disable the WordPress Cron Job

First, disable the WordPress cron job by opening your wp-config.php file and placing the following line of code into it at any place inside the opening PHP tag.

// Place the line of code below in your wp-config.php file
// Defining the constant as true will tell WordPress to stop executing its cron job
define('DISABLE_WP_CRON', true);

(https://gist.github.com/tribulant/ea6bd43800f48f91453b)

The code simply defines DISABLE_WP_CRON to true which WordPress knows of and will then not run cron jobs itself but wait for them to be called instead.

2. Create a Server Cron Job

With no WordPress cron job running anymore, you need to setup a server cron job to replace it. The server cron job will not actually run the scheduled tasks directly, it is just doing the job that your visitors have done up until now, telling WordPress what the date and time is so that it can check if tasks should be fired.

The instructions on setting up a cron job on your hosting will depend on your hosting control panel interface. I would say the most common control panel for hosting is cPanel which is used by most large hosting companies these days and it is great.

So login to your cPanel and go to Advanced > Cron Jobs.

cPanel Cron Jobs

Then under “Add New Cron Job” you can create the cron job. I recommend that you use an interval of 5 to 15 minutes depending on your website and what it does. Use the “Common Settings” drop down menu to select your interval for the cron job.

Add New Cron Job

The best command to use is WGET but you can use other, similar commands if you prefer to change it. Here is the command that you’ll use:

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

(https://gist.github.com/tribulant/5ffc5c7834c4497bb32c)

Be sure to change out the www.yourwebsite.com part with your own domain or URL to your WordPress home page.

WordPress Multi-Site Server Cron Job

Unfortunately the WordPress cron has to be run individually for each site/blog on the WordPress multi-site network.

Because of that, you’ll need to repeat the above process for each site/blog on the network. Or alternatively, you can create a new file named wp-cron-multi-site.php in the WordPress root (same location as the current wp-cron.php file) and put the following code into it:

<?php
if (!defined('ABSPATH')) {
require_once(dirname( __FILE__ ) . '/wp-load.php');
}
global $wpdb;
$sites = $wpdb -> get_results("SELECT `domain`, `path` FROM " . $wpdb -> blogs);
foreach($sites as $site) {
$url = "http://" . $site -> domain . ((!empty($site -> path)) ? $site -> path : '/') . 'wp-cron.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
?>

(https://gist.github.com/tribulant/2afd9afc09e5478b630e)

Then, simply change the cron tab command in cPanel to this:

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

(https://gist.github.com/tribulant/d1581c54fef9f8dc6687)

 

WordPress Server Cron Job Not Working?

Unfortunately, some hosting providers have restrictions on the interval and some hosting providers do not even allow the WGET command to be executed. Sad, but it is happening, and it is a common issue. You have 3 possible solutions here:

  1. Contact your hosting provider and ask them to enable WGET or provide an alternative
  2. Get proper WordPress hosting that won’t restrict you in unfair ways.
  3. Use a 3rd party, online cron job service such as EasyCron for your cron jobs.

And that’s it, you’re done! You’ve replaced the WordPress cron job with a real, server cron job. I hope that you found this useful and that it will increase the speed of your WordPress website drastically.

Earn Money by Referring People

Refer customers to us with your affiliate link and earn commission on sales from your link.

Sign Up
Comments
  1. radiosong on February 27, 2019

    this post is very nice..thank you for it

    Reply
  2. Freethinker on December 4, 2018

    How to check if cron by server is working properly without login to dashboard?

    Reply
  3. Richard Williams on November 1, 2017

    Hi Antonie,
    First I want to say thanks for the great software and support you offer us. I for one, appreciate it, as I’m sure the rest of us do, as well. Anyway, my question is; Can the server cron be run by entering the path to wp-cron.php into the command field instead of the URL, like so: “/home/user/public_html/wp-cron.php”. This is usually the way most of my other cron jobs are created in a cPanel hosting platform.

    I’d like to know what your thought’s are on this.

    Thank you Antonie.

    Sincerely, Richard Williams
    DomainMasters.NET Web Hosting

    Reply
    • Antonie Potgieter on November 8, 2017

      Yes, you can run a PHP script using an absolute path. You’ll do something like php -q /home/user/public_html/wp-cron.php. Just check if it works since you might need to specify the full path to the PHP binary in the command. Your hosting provider can confirm this to you.

      Reply
  4. Tim on October 12, 2017

    Any advice on how to check to see if cron is actually working? Best i could think is to do some action that fires a site email, and see if i get the email. but not too sure.

    I’m wondering because WooCommerce Subscriptions uses pending tasks and for me they will fire 3 months from now, which ummm, i want to make sure they will fire before i wait 3 months to see them not fire.

    Thanks!

    Reply
    • Antonie Potgieter on October 17, 2017

      The best way to check if it actually works is to install a cron manager plugin and see if the cron schedules are fired on time.

      Reply
  5. Ruri on April 3, 2017

    I’m using your multisite script and I’m experiencing lag on my server. My multisite has over 150 blogs. I think it’s cause it’s running all 150 crons at the same time instead of one at a time. Can you modify your multisite script to make it run 1 cron at a time?

    Reply
    • Antonie Potgieter on April 3, 2017

      The WordPress multi-site cron is global but I will digg into it and see if there is a way of running each site individually

      Reply
  6. Revista Ortodoxa on February 18, 2017

    Great article, followed all steps and makes wonders 😀

    Reply
  7. Aditi Aggarwal on November 15, 2016

    Thanks For Sharing Useful Information.

    Reply
  8. Yasitha on September 16, 2015

    I am writing a wordpress backup plugin and I need to run the backup process in every hour.
    When I used wp-crons backup process won’t be run if there are no traffic. Is it possible to schedule real cron for this from the plugin?

    Reply
    • Antonie Potgieter on April 29, 2016

      Yes, that’s what this article explains. How to change the WordPress cron job to a real, server based cron job. So if your WordPress site has no traffic to keep track of it’s cron, the server cron job fires and resolves the problem.

      Reply
  9. SetCron on June 15, 2015

    Try using a webcron service like setcron.com to ping your wp-cron.php

    We have a wordpress plugin and offer 1 free cron job for our free tier. Some other benefits include timezone settings and email notifications.

    Reply
  10. WebNashr on June 12, 2015

    Running all the crons at once in a huge multisite can cause more problems than the default WordPress cron job, it’s a better idea to run crons in a row or at least with a little delay between two sites.

    Reply
    • Antonie Potgieter on June 12, 2015

      Thank you for your input on this and you are absolutely right! Running a WordPress multi-site server cron job like this (especially on a large network) could put massive strain on the server. It may be good to put a sleep or a delay in between the CURL calls as you mentioned.

      Reply
  11. WordPress Newsletter plugin: Set up a Server Cron Job | Documentation on June 6, 2015

    […] the Replace WordPress Cron with a Real Cron Job for Site Speed […]

    Reply
  12. Richard on May 30, 2015

    Hi, nice but what about multisite, do we need to setup cron jobs for every site or just for the main site?

    Reply
    • Antonie Potgieter on May 30, 2015

      Hi Richard,

      Replacing the WordPress cron job with a server cron job on WordPress multi-site requires the process to be repeated for each site/blog on the network.

      I’ve updated the article now with instructions on how to create a custom wp-cron-multi-site.php file and firing that with your server cron job instead. It will use PHP CURL to then open the wp-cron.php file of each site/blog on the network.

      I hope it helps and feel free to post back.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Save 15% On All Purchases

Use this amazing, limited offer and SAVE BIG! Buy any of our WordPress plugins, extension plugins or newsletter templates.

Save 15% On All Purchases

You have Successfully Subscribed!

Pin It on Pinterest