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.
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.
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.
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:
- Contact your hosting provider and ask them to enable
WGET
or provide an alternative - Get proper WordPress hosting that won’t restrict you in unfair ways.
- 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.
I am the owner at Tribulant Software and I have a great passion for WordPress, development, blogging and the Internet in general. Building useful plugins to improve WordPress’ functionality is my goal.
WordPress Plugins
Start selling products, sending newsletters, publishing ads, and more through your own WordPress website using our premium WordPress plugins.
this post is very nice..thank you for it
How to check if cron by server is working properly without login to dashboard?
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
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.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!
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.
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?
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
Great article, followed all steps and makes wonders 😀
Thanks For Sharing Useful Information.
[…] Melalui cPanel : http://tribulant.com/blog/wordpress-2/replace-wordpress-cron-with-real-cron-for-site-speed/ — Melalui Third Party : […]
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?
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.
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.
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.
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.
[…] the Replace WordPress Cron with a Real Cron Job for Site Speed […]
Hi, nice but what about multisite, do we need to setup cron jobs for every site or just for the main site?
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 thewp-cron.php
file of each site/blog on the network.I hope it helps and feel free to post back.