A Better WP Cron

Let’s start out at the beginning. Just what is cron anyway?

Cron is… a utility that Unix and Linux use to schedule commands and scripts that should run on a regular basis in the background without any user interaction.

A cron job is… an individual task, which is set using cron to run at specified time inetervals, this could be at a fixed time each day, on certain dates or at specified intervals, i.e. once per hour.

So, pretty much every linux server everywhere is running cron jobs of various kinds all of the time. Many pieces of software will throw their own cron jobs into the mix and that’s just fine, in many cases cron jobs are pretty much set and forget (until it doesn’t work and nobody notices – which is why we monitor cron jobs).

WordPress and wp-cron

Then we get to WordPress, which doesn’t use cron in quite the same way. WordPress has a lot of tasks that need a trigger to run, things like sending out system email, publishing scheduled posts, checking for updates and running backups, these are all things that are triggered by a cron job.

The thing is that instead of using a time scheduled server cron job, WordPress uses a file called wp-cron.php which gets triggered every time someone visits the site. In theory this kind of makes sense, but in practice it is a bit flawed because:

  • If you have a site with very low traffic, then wp-cron rarely runs, because nobody is visiting and triggering it. This is probably not a huge problem, if so few people are visiting then backups, updates, etc. are probably not so important to you.
  • If you have a site with moderate or high traffic, then wp-cron runs far more often than it actually needs to, and every time it does it creates a new https connection as if a second visitor was accessing the site, which creates a lot of unnecessary load on the server and potentially slows things down.
  • It’s also a potential security issue, as it’s a very open target to DDOS attacks.

So, the best thing to do is to simply disable wp-cron and set up a regular (time based) cron job to replace it. You can do this either by setting up cron jobs directly on your server or by using an external cron service to do it for you.

What we do

For every site hosted at S4, we disable wp-cron and set up a real cron job automatically. These cron jobs every 10 minutes by default, which we think is the best balance between overkill and not missing anything. If you really need to change this frequency for any reason you can find how to do it in our knowledgebase.

One of the biggest problems with cron is that very often you have no way to know whether your jobs ran or not. You won’t know it isn’t working until something breaks, Just in case, we monitor your site cron jobs and our server admins get an alert if any of your jobs fail to run or are taking longer than they should.

What you can do

If you aren’t hosting your sites with us, and want to set up real cron jobs instead of wp-cron then there are a couple of very simple ways you can do that.

First you will need to disable wp-cron which you can do by inserting the following line into your wp-config file.

define('DISABLE_WP_CRON', true);

Then you will need to create a scheduled cron to replace it. You can either do this by creating a server cron job or by using a third party service.

If you want to set up a cron job on your own server (which is what we recommend) then you will find the setting to do that in CyberPanel, CPanel, Plesk or whatever other control panel your hosting provides pretty easily.

The only thing to be sure of is that you get the cron notation right for the time scheduling. There is a fantastic and easy cron expression editor that will help you make sure you get it right at crontab guru.

You can of course also do it from the command line but if you aren’t using a control panel to manage your site then you either know how to setup cron from command line already or know how to find out how to. 🙂

If you prefer to use a third party service instead of playing with any more settings then take a look at Easycron, which has free and paid plans.

Leave a comment