Stop wp-cron (WordPress cron Jobs) From Firing on Every Page Load

Scheduled Tasks Slowing You Down?Have you ever notices that sometimes WordPress can load slow? This is particularly true if no one has gone to your page in a while and you try to log into the WordPress Dashboard. Why is this? Is it a cache issue? Is something wrong with my site? Why does it stall on the first page?

Well, in nearly all cases the reason isn’t a problem at all but by design (I’ll leave room here for the rare cases when something is actually wrong but that’s for a different day).

The reason is wp-cron. This is WordPress’ task scheduler that takes care of things like checking for updates, publishing scheduled posts, and a whole lot of other functions depending on your configuration.

Now by itself wp-cron isn’t a problem. In fact without it most sites would have a whole set of new problems. The problem with wp-cron that slows your site down however is two-fold. First, it runs on every single page load. This means that whether it is needed or not it is running and like all running scripts can take some time to process. Second, if the site hasn’t been loaded in a while it will have  a whole lot of missed tasks to finish up which can greatly compound the loading time.

So how do we fix this? How can we get WordPress to run our tasks on a regular basis without doing so on every page load? The answer comes in two parts. First, we’ll disable wp-cron from running within WordPress whenever a user loads a page. Second, we’ll set up wp-cron to instead run at a regular interval in the background on our server. Put together this technique will speed up the load time for our users and make sure that wp-cron is running an maintenance tasks whether anyone has come to our site or not.

Let’s get started.

1.) Open up wp-config.php

You’ll want to find and open the wp-config.php file which is located in either the main folder of your site or the folder directly above it.

2.) Add the following line

This should go somewhere before the “That’s all, stop editing” line.

3.) Save the file

Now wp-cron will no longer fire every time someone loads your pages. We’re half way there.

4.) Create a system CRON job

Cron is actually the task scheduling system used in *nix systems to schedule all sorts of functions on a regular basis. What we’re going to do is schedule cron to run every 10 minutes via cron (sorry Window’s users you’re on your own with this one). This should run it often enough to keep tasks from building up while keeping it from firing so often that it is actually hurting our load times.

If you’re on a shared hosting account with a service such as HostGator you will need to edit the cron job from your control panel. If however you’re on a VPS host or a dedicated server you can use the following.

a.) log into the system via terminal or another ssh client

b.) Enter the cron editor with the following command:

crontab -e

c.) Schedule wp-cron.php to run every 10 minutes by entering the following command. Note that the path to your wp-cron.php and your path to php may be different.

0,10,20,30,40,50 * * * * /usr/bin/php /home/webuser/public_html/wp-cron.php >/dev/null 2>&1

d.) save and exit (the procedure for this may vary depending on what text editor you’re using to edit crontab.

That’s it. Now your scheduled tasks will fire every 10 minutes regardless of whether your site gets any traffic.

About Chris Wiegman

Chris is a developer for iThemes where he works on the iThemes Security and iThemes Security Pro WordPress plugins. In past roles he has served as a teacher, blogger, manager and even an airline captain. He resides in Austin, TX with his wife Joy and their four-legged children.

Find Chris on Facebook, , LinkedIn, and Twitter.

Comments

  1. says

    Thanks for the tips, I’ve just apply it on my shared hosting cpanel and its works, I get an improvement on my page load time. is that the 10 minutes the best ideal setting? and how if I set it on higher number. ex 20 minutes? thanks..

  2. says

    I came up with 10 minutes after experimenting with this site. 10 minutes seemed adequate considering my traffic load. Try experimenting on your own site and see what you get.

  3. says

    Wow, I had no idea on how bad wp-cron was. I (wrongly) assumed that it wouldn’t be loading on every page load but just occasionally, although I was stumped on “when” or “how” that would happen.

    Now I’m starting to wonder how many similar issues exist on WP scripts running “on every page load” beyond wp-cron that could be moved elsewhere… but perhaps core developers/plugin writers/theme writers all go through wp-cron for that, and, as such, your solution is universal.

    Your article has been an eye-opener, thank you very much!

    • says

      Ok, stupid question. When running the command from the shell — just to test out things — I get the following error:

      PHP Warning: require_once(./wp-load.php): failed to open stream: No such file or directory in /home/webuser/public_html/wp-cron.php on line 26

      PHP Fatal error: require_once(): Failed opening required ‘./wp-load.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /home/webuser/public_html/wp-cron.php on line 26

      This is because wp-load.php is referred relatively to where wp-cron.php is.

      The solution seems to be to change the directory first , e.g:

      0,10,20,30,40,50 * * * * cd /home/webuser/public_html/ && /usr/bin/php /home/webuser/public_html/wp-cron.php >/dev/null 2>&1

      I’ll let you know if this really works as intended :)