Scheduling the publication of posts in WordPress , checking Plugins for updates, creating backups - these are all typical examples of tasks that happen repeatedly. And this is where WP-Cron comes into play in WordPress . What this means and when server-side cron jobs make more sense, we explain in this post.
As you know, your computer is constantly performing countless calculations with ones and zeros at breakneck speed, all of which have a job to do. Many of these are tasks that need to be repeated over and over again, much like brushing your teeth. On that note, I have a question for you:
If you could automate brushing your teeth, would you?
Your WordPress website has automated maintenance tasks it needs to perform on a regular basis. In this article, we'll look at what these tasks are, what problems they can cause, and how RAIDBOXES makes it super simple to manage them.
Every time your local computer displays a popup message telling you an update has been found or an email has arrived in your inbox, it's most likely a cronjob that is working behind the scenes.
What is Cron?
Cron is a command for task scheduling that is built into Linux-based systems, e.g. Ubuntu, Debian, Mac OSX, as well as servers. For Windows, this feature is called "Task Scheduler".
Here are some docs on how to use the Cron feature in the various systems:
- Windows - Task Scheduler (uses a graphical user interface [GUI] for more user-friendliness)
- Mac - Launched Jobs and Cron Jobs (uses Terminal and requires some knowledge of Bash scripting)
- Server - Example: cPanel - (uses either an integrated GUI or terminal)
What does Cron do anyway?
The purpose of Cron is to perform repetitive tasks, e.g. checking for available updates or sending and receiving emails. This is usually set up for you by default. But the more software you have installed on your computer, the more jobs are likely to be added. As an example, the screenshot below shows a Cron job in the Task Scheduler in Windows 10.
You're wondering how Cron works? It works by scheduling tasks based on the time of your device and can be set up by the minute, hour, day, week, month, and year. In Cron, you set a specific time for an action to be performed.
What is WP-Cron?
WordPress Cron, also known as WP-Cron, is a PHP file that is usually located in the root directory of your WordPress installation.
And what does WP-Cron do?
The WordPress cron file (wp-cron.php) contains the automated tasks that your WordPress installation needs to perform. For example, things like checking for updates to Plugins and WordPress or publishing scheduled posts. For more information, see the documentation "What is WP-Cron" in the official WordPress Plugin Handbook.
WP-Cron versus Cron
The main difference between WordPress cron and the cron scheduler is that wp-cron.php is only executed when a user visits your website and triggers a page load. This means that WordPress requires an HTTP/HTTPS request from your website to run scheduled tasks. The cron task is then queued to be executed.
You're probably correctly asking yourself:
Why does WP Cron only run when a user visits my site!
Well, many WordPress hosting providers only offer shared hosting, which usually means your host won't give you access to the cron command. If they did, you'd have access to the server commands and all data on the server would be potentially compromised. WordPress Cron is therefore the workaround for this problem.
The second difference is that when using Cron, you can set specific times (for example, 17:00 daily to run a task). In WP-Cron, on the other hand, you set intervals (for example, 14:00 & interval X after that). WP-Cron then executes the task at those intervals as long as a user has visited site . For more info, see also: Understanding WP-Cron Scheduling.
WP-Cron and performance
WP-Cron is only triggered whenever a user visits your website and this can cause problems for both small and large websites. Let me explain why.
Small & geographically specific websites
The problem for smaller websites with little traffic is that the cron task isn't executed often enough. Say, for example, you have a site for a small tour guide company only operating in one city and you want your website to make a backup at 2:00 a.m. It's very unlikely a user from your target audience will visit your website at 2:00 am and trigger the Cron job. And if it's low season, you might not get any visitors for days at a time.
Larger websites & global reach
However, for sites with a large number of users, WP-Cron can cause some performance issues and even make your site vulnerable to a DDoS attack. Well, if you probably just panicked and grabbed your mouse or coffee cup at the mention of a DDoS attack: don't worry too much, because there is a solution for everything.
What causes the problem?
The amount of traffic causes the problem. Traffic is a must for large websites and generates business, so limiting it is not the way to solve the issue.
Remember: Every visit to the site causes WP-Cron to be triggered - and this puts a strain on your server because it requires resources to run.
Is there a better way?
Fortunately, the answer is yes. While we can't simply remove web traffic, open physical stores around the globe, or change the way WordPress is set up, we can change what makes WP-Cron run.
Instead of running wp-cron.php based on site visits, we can set up a Cron task on the hosting server that runs WP Cron at certain intervals. Let me show you how to do this.
Setting up server-sided execution of wp-cron.php at RAIDBOXES
To make your work as easy as possible, the team at RAIDBOXES has built this feature directly into the RB Dashboard. You literally need five clicks to set up the server-side execution of WordPress Cron jobs! If you're a developer looking to save time, this is one of the easiest things to set up.
To enable server-side Cron jobs in your RAIDBOXES Dashboard, first go to the settings of your BOX and then click on "Cronjobs" in the sidebar.
Now you will see all the options for setting your Cron jobs.
Make the following configuration in the Cronjobs area:
- Turn off "Run WP-Cron" (this prevents WP-Cron from being triggered by website visitors).
- Turn on "Run WP-Cron server-side" (this adds a cron task to the server to trigger wp-cron.php at the interval you specify).
That's all there is to it!
If you turn off Run WP-Cron and also the Run WP-Cron server-side option is disabled, no more cron jobs will run on your site .
You don't need to contact your host, go into cPanel, or edit your wp-cron.php file manually. At RAIDBOXES, you only need a few clicks to monitor and manage your cron jobs – directly in the RB Dashboard. So there's no need to install an additional plugin to view your Cron jobs either.
If you'd like to make further changes, you can also specify the interval at which the wp-cron.php should be executed in the RAIDBOXES Dashboard.
There's a lot of speculation about the "best" interval for your cronjobs. We usually recommend not going below 5 minutes.
Edit your WordPress Cron jobs
If you don't want to install a plugin to edit your wp-cron.php file, you don't need to! At RAIDBOXES, you can easily manage your Cron jobs via the RB Dashboard as described above. Of course, you can also access your Cron jobs via SSH. Once you've connected to your account, you can edit wp-cron.php in the root directory of your WordPress website.
If you want to know how to make your workflow even more efficient with SSH, it's best to check out our article "The Perfect WordPress Development Workflow with Git & SSH".
Currently the most popular Plugin for managing WP cronjobs is "WP Crontrol". You can also search and install the Plugin directly in your WordPress at Plugins and install it.
On WP Crontrol's Plugin-site at WordPress .org you can also find helpful documentation on how to use the Plugin properly.
Manual setup of the server to trigger WP Cron (not necessary at RAIDBOXES)
With a bit of set up, you can also configure your server to run wp-cron.php at certain intervals.
- Edit the wp-cron.php
- Disable your wp-cron.php file by adding the following code to the file:
Now wp-cron.php will no longer be triggered when a user visits your website.
- Add a Cron job to your server
In your server environment
Depending on your hosting environment, one of the two options listed below may apply to your specific situation:
- If you're using a shared WordPress hosting service and you don't have access to the Linux commands yourself, contact your hosting provider and ask them to add a server-side Cron trigger for wp-cron.php.
- Or, if you're running your site with cPanel and have access to your command line, check out the appropriate cPanel documentation.
The WP-Cron file is quickly overlooked when building a WordPress website but it can lead to noticeable errors when your site starts to attract more visitors.
Prepare WordPress for traffic peaks
You expect a lot of traffic on your WordPress website, e.g. due to a marketing campaign? Then you need the right preparation! Our Head of Development Matthias shows you which 13 measures you can take to prepare your WordPress for high loads.
I hope this article helps you understand what the WordPress Cron function is and how it works. Used properly, it can help you automate important tasks reliably so you can focus on running your business.
Our goal is to make managing your WP Cron jobs as easy and convenient as possible via the RAIDBOXES Dashboard. This fast and direct access allows you to optimize your development and management processes even further.
Do you have more questions about WP-Cron or our dashboard? Feel free to leave us a comment! You want to be informed about new posts on WordPress ? Then follow us on Twitter, Facebook or via our newsletter.