What Is WP Cron & Setting Up Server-Side Cron Jobs

wp cron featured image

Looking to set up external, server-side WP-Cron Jobs for your website? If so, you’ve come to the right place. Without further ado – let’s dive right in. 

WordPress is one the most popular CMS platforms, and it’s no wonder – out of 1.89 billion websites existing on the internet, over 807 million of them are using WordPress, which makes a whopping 42.7% of the internet. Some sources even suggest that WordPress holds almost 40% of the market share. It comes equipped with the tools necessary to create your website, which seems very simple to use.

As popular as it may be, it does have its downsides. WordPress has a built-in feature called WP-Cron, and this handles the scheduling of time-based tasks. Tasks such as scheduling posts, checking for updates, and the like. This cron system is known to have issues that we’ll delve more into in a bit. Setting up server-side WP-Cron Jobs helps minimize issues with WP-Cron as well as improve your site’s performance. 

So, without further ado – let’s dive right into it…

What is WP-Cron (WordPress Cron)?

Before we run through the steps on setting it up, let’s first break down what WordPress Cron is and WordPress Cron Jobs are… 

Cron is a standard UNIX utility for scheduling task execution (script or command) at a specific time, date, or interval. A cron job is a task that it’s going to be executed. Its sole purpose is to automate repetitive tasks so that you can use your time more productively.

WordPress has its cron system for scheduling tasks. WP-Cron handles these tasks. Although the main idea and name come from UNIX cron, WP-Cron works differently and instead uses intervals for task scheduling. 

The default time intervals provided by WordPress are hourly, twice daily, daily, and weekly. Unfortunately, these time-based tasks are dependent on users visiting your site, which means that WP-Cron will only execute if a user visits your website.

In other words, WP-Cron only executes when your webpage loads up. It makes WP Cron unreliable and your site slower as well, which is why many users prefer a server-level cron job over the standard (and often default) WP-Cron jobs. 

Now that you know what WP-Cron is, let’s dive into setting up a WP-Cron job.

How to Set Up a WordPress Cron Job

To schedule and handle WP Cron events, we can easily make use of the free WP Crontrol plugin. It enables us to view and control what’s happening in the WP Cron system.

wp cron events in wordpress

With over 100,000+ active installations, this cron-management system can easily make your life a whole lot easier. It shows what Cron jobs should run and is an excellent step in understanding what’s going on.

You can download WP Crontol from the WordPress website or simply go to the plugins section on your site and browse for WP Crontrol.

WP Crontrol Features:

  • View all cron events and their arguments, run times and recurrence
  • Edit, delete, and immediately run any cron events.
  • Add new cron events.
  • Add, edit, and remove custom cron schedules.

To add a new cron event all you have to do is go to the “Add Cron Event” tab and fill in the mandatory information

wp crontrol plugin for adding new cron event

There are other ways to schedule WP-Cron events, such as creating custom hooks. You may also refer to Tom Mcfarlin’s article on defining a WordPress cron job

The official WordPress plugin handbook also provides a neat guide on scheduling WP cron events

However, for beginners, WP Crontrol should be the easiest way to go.

Setting Up Server-Side WP-Cron with RunCloud

There are a handful of reasons to find alternatives for WordPress’s built-in cron scheduler (wp-cron). You can read more about why you might want to disable wp-cron here

For small-scale websites, wp-cron should work perfectly fine. Yet, for larger companies, the scheduler is known to cause issues given the nature of its behavior. Therefore, the best solution is to set up the cron events yourself.

In this post we will cover multiple ways how to set up server-side WordPress cron events, starting with the one we recommend the most – setting up a server-side cron job with RunCloud.

RunCloud gives you the ability to control your server-side WordPress Cron events simply by using your RunCloud dashboard.

The process is super simple – navigate to the server that hosts the web application you’re looking to set up a cron job for and then in the sidebar, select Cron Job.

setting up wp cron job via runcloud

There you will have the option to set up your cron job task:

adding wp cron event with RC

In the field Job Label give your cron job a name.

In the User field, you can select the system user responsible for this cron job.

If you don’t have much experience or knowledge when it comes to the Vendor Binary option, it’s best that you leave the field on default settings. /bin/bash is the place where the job will be called from when executing.

The Command is where you will add the command you want to run including the script of what you want to run. For example, if you want to create a new log file every day with some type of information, you would add a script like this:

wget -q -O -https://blog.runcloud.io/wp-cron.php?doing_wp_cron >/dev/null 2>&1

This is a script that tells the computer, “run the random.php file every minute”. Since RunCloud does not require you to enter this into the Terminal, you can use the RunCloud dashboard to do the job for you.

“Run in” is where you would designate how often the script should run. While running it every minute is fine, it really depends on what the script is actually doing. Bear in mind that the longer the script, the more server work will be needed.

And that’s pretty much it – after completing these steps, you’ll successfully set up the WP cron job through the RunCloud dashboard!

How To Set Up Server-Side WordPress Cron Events in cPanel

In the event that you aren’t using RunCloud to manage your server infrastructure yet, here’s how you can set up server-side cron jobs in cPanel. 

1. Disable WP-Cron

Disabling wp-cron is pretty straightforward. Firstly, log into your server, then locate wp-config.php. Before the line that states: “That’s all, stop editing! Happy blogging.”, add the following code:

code for disabling wp cron

Save and close the file.

2. Set Up Cron Event in cPanel

In your cPanel (or control panel) account, go to Advanced and navigate to Cron Jobs. Log in to the cPanel account for the site you’re working on, then locate Cron Jobs:

c panel settings for controlling cron jobs

3. Set Up the Interval

Navigate to the Add New Cron Job section. There, you’ll find an interface for setting the event interval. You’ll see a dropdown for the Common Settings. Choose Twice Per Hour:

setting up cron intervals

4. Add the Event Script

Next, we have to add an Event Script. Add the following line in the command field.

wget -q -O - https://domain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

script for disabling wp cron

This portion

>/dev/null 2>&1

disables email notifications.

5. Test If It Works

Lastly, with any post-setup, you should check to see if it works. To tell if it’s working or not shouldn’t be too hard to say. If something isn’t updating automatically or the way it should be, you can proceed to troubleshoot your work.

Setting Up Server-Side Cron for WordPress Multisite

Each site in a Multisite network must make calls to cron events. Fortunately, a simple script created by Bjørn Johansen already achieves this.

This whole setup shouldn’t take too long. Give yourself around 10 minutes, and you should have a working server-side cron for WordPress Multisite in no time.

Here are five steps to setting it up.

1. Disable WordPress-Cron

As mentioned earlier in this article, add the following line to your wp-config.php.

define('DISABLE_WP_CRON', true);

2. Create a Script for WP Multisite

Next, insert the script created by Johansen. Then create a new file named run-wp-cron.sh and add the following:

#!/bin/bash
# Copyright © 2015 Bjørn Johansen
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
WP_PATH="/path/to/wp"
# Check if WP-CLI is available
if ! hash wp 2>/dev/null; then
echo "WP-CLI is not available"
exit
fi
# If WordPress isn’t installed here, we bail
if ! $(wp core is-installed --path="$WP_PATH" --quiet); then
echo "WordPress is not installed here: ${WP_PATH}"
exit
fi
# Get a list of site URLs
if $(wp core is-installed --path="$WP_PATH" --quiet --network);
then
SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" | sed 1d`
else
SITE_URLS=(`wp option get siteurl --path="$WP_PATH"`)
fi
# Loop through all the sites
for SITE_URL in $SITE_URLS
do
# Run all event hooks that are due
for EVENT_HOOK in $(wp cron event list --format=csv --fields=hook,next_run_relative --url="$SITE_URL" --path="$WP_PATH" | grep now$ | awk -F ',' '{print $1}')
do
wp cron event run "$EVENT_HOOK" --url="$SITE_URL" --path="$WP_PATH" --quiet
done
done

Note: You must set the WP_PATH to the full path of your WordPress site installation, i.e.

/home/runcloud/webapps/mywebapp

You mustn’t leave a trailing slash “/” in your path.

Once finished, save and close the file.

3. Execute the Script

Now, we have to execute the script. To do this, run:

chmod +x run-wp-cron.sh

Or you may also test the script like this:

./run-wp-cron.sh

4. Setup The Cron Job on cPanel

In the cPanel of your WordPress site, enter the following in the time field:

*/30 * * * *

The “30” indicates the cron job would run once every 30 minutes. You may set up an even more frequent value, but note that more extensive WordPress multisite installations may need more time for all cron processes to complete. Also, you may encounter some problems if you set the timer too low.

Next, in the command field, enter the following:

/kunder/group/user/public/run-wp-cron.sh

Replace the path with the current path to your WordPress site installation.

5. Turn Off Email Notifications (Optional)

If an email notification isn’t required when a cron job executes, you may opt to turn these off. Select the newly created cron job in the list of this page and select “Do not send email” in the dropdown box below. 

Afterward, click the “Update” button located at the bottom of the page.

Note: If you still manage to receive emails regarding cron jobs even after silencing them, the problem might be one of the configured cron events. It would be best if you double-checked with those to solve this problem.

You Might Also Ask

What triggers WP-Cron?

WP-Cron does not run continuously. The wp-cron.php by default activates on every page load, which on websites with big traffic can cause problems. 

The issue is when a site doesn’t have enough PHP workers. When a request comes in and WordPress spawns the cron, the cron must wait for the worker before completing. It leaves the cron sitting in place.

The reverse is also true. If a site doesn’t have enough traffic, websites may miss schedules because no one has loaded the page.

The better approach is to disable WP-Cron and use the system cron instead. The official Plugin handbook also recommends this since the system runs on a predefined schedule.

Where are WordPress cron jobs stored?

WordPress cron jobs are stored in the database inside wp_options under the option_name cron. You can obtain the array with: _get_cron_array() or get_option(‘cron’).

How do you test if WP-Cron is working?

Open your wp-config.php file to check if there is a DISABLE_WP_CRON constant defined as true. If that line does exist, remove it.

One way to check scheduled tasks and see if WP-Cron is working as expected is with the WP Crontrol plugin.

How do I know if a cron is running in WordPress?

Using the WP Crontrol plugin:

Upon activation, visit the Tools and then Cron Events page to manage your cron settings.

Doing this would show the list of all scheduled cron events on your screen. In the first column, you will see the name of the hook that runs the corn.

How often should I run WP-Cron?

Depending on the services you use, it might be subject to different time intervals. However, on average, a job that calls your site’s wp-cron.php script every 15 minutes should be all that you need.

If there are schedules required to run more than once every 15 minutes, it would be best if you adjust it accordingly.

Is WP-Cron enabled by default?

Yes, the WP-Cron is enabled by default because WordPress features rely on it to execute scheduled jobs. If in case the cron is disabled, you can also enable WP-Cron by editing wp-config and adding the following line:

define(‘DISABLE_WP_CRON’, false);

Setting this to true would disable the corn.

Can I delete WP-Cron?

No, it’s not possible to delete WP-Cron since it is the built-in cron system for WordPress. However, it is possible to disable WP-Cron and use the alternatives explained in this article.

An option to delete cron events using the WP Crontrol plugin is also available.

To delete all cron events using the WP Crontrol plugin:

  1. Select Tools.
  2. Select Cron events.
  3. Click Choose All.
  4. Then Delete.

Setting Up Crons Easily

Although it is a built-in feature of WordPress, the WP-Cron system does have its downsides. For large companies planning to manage their website through WordPress, it remains unfavorable.

To circumvent this dilemma, we mention various effective alternatives in this article. With a bit of manual configuration and some pre-made scripts, you’ll have a functional website in no time. If you’re a company looking to create a website, WordPress might be the right way to go. Just take into consideration some of the WP-Cron issues mentioned in this article.

Here at RunCloud we highly recommend following the steps above in creating server-sided cron events yourself. Not only would you save a lot of time figuring things out yourself, but you’d also be able to manage your site directly. Start your 5-day free trial at RunCloud today.

Did the standard WP-Cron work for you? Or did this article help in setting up cron events yourself? Let us know your experiences in the comments below!

Categories: Server Management

Simplifying Server Management

RunCloud is a cloud server management tool that allows you to maintain full control of your server and host multiple WordPress, WooCommerce, Laravel, and PHP applications with fast and easy configuration.

Start Your Free Trial

5 days free trial no credit card required cancel anytime

Leave a Comment

Your email address will not be published. Required fields are marked *