Use System Cron For WordPress using WP-CLI
See how to use system cron for wordpress to reduce loading time on your pages and improve background task reliability
Last Updated: February 15th, 2022
By: Steve
Contents
WordPress is a multi-platform system, meaning that it can run on Windows, Mac or Linux. WordPress sometimes needs to run periodic maintenance type scripts and various plugins also might need to do something every hour or every half day for example.
In the UNIX world, this is performed by a special background process (daemon) called cron. Cron reads a special file called the crontab (cron table) to determine which tasks to run and how often. But Windows does it a very different way, using Scheduled Tasks.
Given that WordPress needs to be portable across all these different platforms, the WordPress developers came up with their own system for running periodic tasks. It’s called the WordPress cron and themes and plugins can hook into this WP-Cron to have their periodic tasks operated.
When a WordPress page is loaded for a visitor, WordPress checks to see if it needs to run any of the periodic tasks. If it does, it runs the necessary WordPress command and then displays the page for the visitor.
The problem with this approach is that some tasks might take a few minutes, or longer, to complete. The visitor will never get their page because they’ll assume the site is down and either go away or they’ll hit refresh. It’s also possible that the specific task being requested will take longer than the page load time allowed by the system. This will result in the task being killed before it’s finished. This is particularly noticeable on shared hosting where the allowed time to run is probably quite short.
I first noticed this when I spoke to SiteGround’s technical support team because I was noticing a lot of timeouts in my logs and indeed when I was trying to access things on my sites. It was because of the WPCron needing to fire off.
Disable WordPress Cron
To disable the WordPress cron, you need to edit the wp-config.php file. If you’re on a hosting provider running cPanel or something similar you should be able to do this using the File Manager that comes with your service provider. If you’re running locally you’ll need to use something like Notepad or Vi depending on if you’re on Windows or Linux.
Add the following line to your wp-config.php file. Somewhere at the top is probably the best, so you can find it again easily if you break it.
define('DISABLE_WP_CRON', true);
Save the file. If you’re going to be using system cron or task scheduling you don’t want WordPress to do it too.
Use the System Cron Instead
To remove the need for a cron job to be called when a visitor connects to your site, you can instead hook into the system task scheduling manager. It differs depending on whether you’re using Windows or MacOS/Linux. I’ll give instructions for either.
Windows Scheduled Task
Prerequisites
Windows doesn’t use a typical Cron process. The instructions I give here will work on Windows 10 Pro – I’m not sure about Windows 10 Home as I’ve not tried it yet. But I’ll have a look when I get a chance and see if there’s any differences.
This tutorial assumes that you’ve set up Apache, MariaDB, PHP and WordPress on your Windows system.
You’ll also need to have downloaded the WP-CLI component and installed it into your PHP directory. In our examples here, we have renamed wp-cli.phar to simply be called wp. Provided your PHP directory is in your path, and the wp php script is in the same place it should work.
Time needed: 15 minutes.
This How To goes through how to add WP Cron jobs to a Windows Installation of WordPress.
- Open Task Scheduler
To open up task scheduler the easiest way is to simply type ‘task scheduler‘ into the Windows search bar. The image below gives you an idea. Clicking Open will bring up the required screen. You don’t need to be an administrator for this. You will need to be logged in as the same user you installed Apache, MariaDB and WordPress though – the latter in particular. - Create New Scheduled Task
Once the Task Scheduler window comes up, you should see a screen that looks like the one below. Click Create Task. This will open up another dialogue box which we will use to customize the task we’re creating. - Set General Settings For Scheduled Task
Choose a name that makes sense. In our case we’ve chosen WordPress Cron as the name as this reflects what we’re wanting the task to be. We’ve filled a bit of a description in too. We do this because when you look through all the scheduled tasks on the system there’s loads. Having a good name and description will help you find yours easier if you need to modify it.
If you’re logged in as the person who owns the WordPress installation you can leave the Author field alone.
We’ve set ours to only run when I’m logged in. This is because I’ll always be logged in. But if you’re setting this up on a server somewhere and you might not always be logged in you’ll need to select the Run whether user is logged on or not option. You may be asked for your password when you select this option. - Setup Triggers – or How Often The Task Will Run
In general most tasks probably only need to be run a maximum of 15 minutely. You can in fact choose longer if you desire. I run mine once every half an hour and that seems to suffice.
To do this, click the Triggers tab at the top of the main Task Scheduler window where you typed in the Name and Description earlier. At the bottom of the tab you’ll see a button that says ‘New…’ – click this button to open a new dialogue that’ll look like the one below.
Choose your task to run daily. You don’t need to worry about the time the task starts on. You also want the task to recur every 1 days, otherwise your tasks won’t run every day. Finally, and a little oddly, you also want the task to repeat every 15 minutes. You can choose a different repeat schedule if you prefer. - Add The Task Action
Click the OK button on the Triggers dialogue box to save those triggers and save them. Then click the Actions tab and choose the ‘New…‘ button as before. This time we’re going to tell the Task Scheduler how to find the program we want to run.
Leave the action as Start A Program.
Click the Browse button
Find your PHP executable. If you’ve installed PHP in line with our tutorial then it should be in C:\PHP\PHP.EXE – which is as it is in the screenshot below.
In the Add Arguments section type the following;/php/wp cron event run --due-now
The /php/wp part of the command is essential to tell PHP where to find the wp script. If you installed it somewhere else in the PreRequisites step then you’ll need to change it to where-ever you stored the wp script instead.
Make sure the Task is set to run from your WordPress installation directory. If you don’t then the wp command will abort with an error saying that it can’t find a WordPress installation.
Once you’ve set that screen up properly – click OK. - That’s it – Test It
From the main Task Scheduler window you can find your newly created task. It should be called WordPress Cron or whatever you chose to call it. Select the task, choose Run underneath the Selected Task header. You should see a quick flash of a command line box start up and disappear again.
In order to check whether it worked though, you’ll need to enable history on the Task Scheduler. Then look for your WP Cron task and look at the History section. Find the Action Completed line in the Category. If PHP finished with an exit value of 0 then it’s probably worked properly. Any other value suggests it hasn’t.
The other way to check is to install something like the WP-Cron Status Checker plugin. It will show you when the last tasks were checked.
Linux / MacOS
Prerequisites
Both Linux and MacOS come with a fairly normal cron process running in the background. Configuring this manually is relatively straight forward – compared to Windows at least. However, manually editing the crontab file is a bit of a nightmare. We’ll go through it though for people who aren’t using a Control Panel from a webhosting company.
Either way you’ll need to have WP-CLI installed on your server. You can use something like curl or wget to fetch the wp-cron.php script but this to me defies the whole point of not having WordPress do it. Particularly if your cron jobs are failing because they’re taking too long and PHP is killing them.
Installing WP-CLI on a Linux or MacOS system is simply a case of following the installation instructions available at https://wp-cli.org/
On my server I have created a shell script that runs the actual commands. I’ve found this to be the easier way. My script contains the following;
#!/bin/bash
cd ~/www/beardeddragonsrock.com/public_html
wp cron event run --due-now
I’ve saved that script as run-bdr-cron in my home directory. The initial cd command is essential since the script is stored in my home directory. The WP-CLI command expects to find an installation of WordPress when it’s running – if you don’t do the cd command first it won’t find it.
Manual Crontab Edits
Manually editing the crontab is a little bit of a unique experience, but once you get the hang of it it’s not too arduous. At any time you should be able to type man 5 crontab to get the format of the file if you forget.
Get a Terminal prompt up and running. Mac users can do this from the Utilities subfolder on the Finder Applications. Linux users, you’ll have to find out how to do that on your distribution. It’s probably fairly straight forward though.
Once you have the terminal window up – type the following command;
crontab -e
This will open an editor – likely vi (which is not fantastically easy to use!) where you can add the following line to run your WordPress cron every 15 minutes.
0/15 * * * * $HOME/run-wp-cron
This line will need modifying to match your directory structure and the name of the script you saved earlier of course.
Understanding The Crontab Format
The format is quite straight forward once you get your head around it. Think of it like a table (hence, crontab). The first column indicates the minutes within the hour that the command will run. In the example above we’ve said 0 minutes and repeat every 15 ( 0/15 ). You could put this into crontab in an alternative fashion such as 0,15,30,45 to achieve the same. The next column is the hours within the day. The * here signifies every hour. But you could have put 0/2 for example. This would have signified starting at midnight and every 2 hours.
The third column is the day of the month. This can be useful if you only want a job to run every so often, for example on the first and the 15th you would put 1,15 in the third column. We want our job to run every day, so we simply put *
The fourth column is the month of the year. Again, we run every month, so *
Finally, the fifth column is the day of the week. You may want a job to only run on a Sunday for example. In which case you can put 0 or 7 in this column, or you can write Sun for Sunday.
The very last column is the command itself.
Using Your Hosting Control Panel
Most web hosts provide cPanel or something similar. I use SiteGround so I will demonstrate their control panel here, but my understanding is that their control panel is mostly just a skinned version of cPanel so yours should be similar.
Using a webhost control panel is significantly easier than trying to understand the manual crontab – but I include that in the article in case anyone is looking for it.
To begin, you’ll need to navigate to the cron jobs screen. With SiteGround this under the ‘Hamburger menu’ on the left and then under the Devs option. This might be different on your hosting provider’s control panel, but should be available somewhere fairly easily accessible.
Clicking the Cron Jobs menu item will then take you to a new screen where you can enter the command to run and select how often to run it. There’s no need to understand the format of the crontab file if you’re using this method as it will all be entered for you.
You will still need to have created a script that you store in your home directory though, since you cannot tell the crontab manager to start from a specific directory. Although you can queue commands within crontab, I find it just easier management to create a script and call that every half hour. If you want to add other things to do every half hour then you can just edit the script rather than having to find the crontab entry and modify it.
The crontab screen looks like the image to the right on SiteGround. If you’ve been reading up to this point it should look fairly self explanatory too 🙂
Hopefully you’ve found this article useful – if you have any questions or comments then please let us know below. If you’re looking for web hosting for your WordPress site we highly recommend SiteGround. You can read a review of SiteGround Hosting by clicking the link.