How You’re Losing Money With WordPress Cron (WP-Cron) and How to Fix it

We work with a lot of clients on a daily basis and we have noticed having the ability to schedule tasks and guarantee that it runs at the set time is key to running any business, this is even more important with our e-commerce clients.

Making sure automatics renewals, email notifications, billing charges, in/out of stock notifications can be very important to any business. In our experience, we have seen clients rely on the basic WordPress scheduling set up to managing all these time critical tasks.

In general, this is a fundamental problem that computers are designed to solve. Most systems come with a job-scheduler called a Cron. A Cron is essentially a time-based job scheduler in Unix-based computer systems which run at set times and intervals. The word cron gets its name from the Greek word for time called “Chronos”

WordPress, which has grown to be more than just a blogging platforms thanks to the additions of various plugins like Woocorrmce comes with an inbuilt task scheduler system called WP-Cron.

WP-Cron is basically a feature in the core of WordPress which just like operating system cron, regularly checks to see if WordPress itself needs updating, or if any of your plugins and themes have updates available.

This feature is named after the Unix/Linux-based Cron service that runs various system jobs at specified times and intervals as described above.

The problem is that the WordPress implementation of a cron isn’t a true cron service. There is no guarantee that your task or job that you have scheduled will run exactly when it to.

The main reason why this is so is that WP-Cron is triggered when a user visits a page on your website.
The advantage of having a cron system like this is that it’s very easy and you don’t have to fiddle with cron jobs to get WordPress’ tasks running, it’s just part of the web application which is really great for non-critical parts of your application.

The problem is this that if you need to rely on this for critical parts of your application and your website only gets occasional traffic, then your jobs and schedules tasks will not be run when they need to be. This is obviously concerning when you have money involved.

The good news is that if you want WordPress tasks to run reliably, you can disable the built-in trigger to run cron jobs, and instead configure your server’s system cron service to trigger the WordPress scheduler.

Related:  Using fail2ban to Secure Wordpress by Automatically Blocking Repeated Login Attempts

To be fair, the WordPress official documentation confirms this so it’s on you if you are losing money for not knowing this. If you need us to do a free website review just reach out to us with any concerns. We have dedicated a team of WordPress experts who are always ready to help.

WP-Cron does not run continuously, which can be an issue if there are critical tasks that must run on time. There is an easy solution for this. Simply set up your system’s task scheduler to run on the intervals you desire (or at the specific time needed).

In the rest of the post, we are going to show you how to disable the built-in WordPress scheduler called WP-Cron and use the system cron to run scheduled or critical jobs at the interval you want.

Firstly, we are proceeding on with the assumption that you are running a Unix/Linux based operating system and you have access to the terminal. You do not need to be super user to make changes to the cron job. Every user on Linux has access to their own cron job.

To begin, you need to first disable the default cron behaviour by adding the line below from the your main wp-config.php. If you are not familiar with PHP, what the line below does is that is just tells WordPress not to use the builtin cron mechanism.

define('DISABLE_WP_CRON', true);

Next, you’ll need to add a task to your system’s crontab telling it to run the cron script inside your WordPress installation. To do this, run the following command on your web server:

crontab -e

In the later paragraphs we will go over the basics of cron. The -e at the end basically means you are about to edit or make changes to the cron settings.

What this does is it opens the crontab file and you need to add a line to this file that indicates what command to run and when it should run. Every cron command is made up of 6 parts. The first five parts indicate when the command will run. The five pieces are (in order):

minutes (0 – 59)
hours (0 – 23)
day of the month (1 – 31)
month (1 – 12)
day of the week (0 – 6)

If any of these pieces are set to *, it indicates “all” for that part of the time configuration. The last piece of the cron command is the command to be run. An example cron configuration line looks like:

0 0 * * * wget https://full-url-to-your-website.com/wp-cron.php

As an alternative if you don’t have any server variables set, you can run it using the php command line utility like the command below:

0  0 * * *	www-data	php /var/www/my-web-site-root/wordpress/wp-cron.php > /dev/null 2>&1

What the above command does it to basically run the cron.php script at midnight every day. Thos way you are guaranteed that your scheduled tasks will run every day at midnight. The wget command is used for retrieving content from the web from the command line. Since it is non-interactive, it’s perfect for command line use.

Related:  How to Include Category and Subcategory in WordPress URLs

Now we want to give a basic explanation of cron. A cron job consists out of six fields:

minute hour day month day command

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)

When specifying day of week, both day 0 and day 7 will be considered Sunday. A field may be an asterisk (*), which always stands for first-last. Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter), e.g. sun or SUN for Sunday or mar / MAR for March..

As examples we want to take a look at the following jobs and see what they mean.

1,2,5,9 - means every first, second, fifth, and ninth (minute, hour, month, ...).
0-4,8-12 - means all (minutes, hours, months,...) from 0 to 4 and from 8 to 12.

We hope you have enjoyed reading this post about crons in WordPress.

Feel free to reach out to our support crew in the case of any issues, we offer 24/7 WordPress support at a ridiculously low price.

If you liked this article, then please subscribe to our mailing list for WordPress video tutorials. You can also find us on Twitter and Facebook.

How You’re Losing Money With WordPress Cron (WP-Cron) and How to Fix it
5 (85%) 916 votes