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?

This is quick and easy, let me show you how! 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.

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:

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:

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

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 true 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 silly 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.

Premium WordPress Plugins

Start selling products, sending newsletters, publishing ads and more through your own WordPress website using our premium WordPressp plugins.

Browse
Comments
  1. 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
  2. […] the Replace WordPress Cron with a Real Cron Job for Site Speed […]

    Reply
  3. 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
  4. 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
  5. 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
  6. Aditi Aggarwal on November 15, 2016

    Thanks For Sharing Useful Information.

    Reply
  7. Revista Ortodoxa on February 18, 2017

    Great article, followed all steps and makes wonders 😀

    Reply
  8. 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

Leave a Reply

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

Pin It on Pinterest

Save 30% On All Purchases

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

Save 30% On All Purchases

Please check your email inbox for your coupon code to purchase now.

%d bloggers like this: