You don’t necessarily need to be a technical wizard to build a website, but this doesn’t mean there is no learning curve ahead of you. If you use a CMS like WordPress, you’ll probably get to grips with the app’s basic functions fairly quickly. However, if you want to develop a successful project, you’ll need to have a better understanding of how it all works.
Among other things, this means figuring out what cron jobs do and setting them up correctly. Let’s see if we can help.
What is a Cron Job?
Although most of them remain hidden, your computer performs tasks all the time. There are many examples. Your email client regularly connects to the mail server to check for messages, your operating system goes online to ensure it displays the correct time, etc.
If you use Windows, all these processes are controlled by the Task Scheduler. If you run a Unix-based OS like Linux or macOS, they are taken care of by cron.
Cron is a command-line utility that runs commands and scripts at fixed times or preset periods. In addition to app-specific jobs, it’s responsible for automating many of the tasks related to the optimal performance of the operating system.
Cron jobs run periodically, and the schedule is kept in a crontab file or cron table. Every user on the machine can have their own cron table, and the system-wide crontab is usually located in either /etc or /etc/cron.d/.
Crontabs can be edited with a text editor, and each line in them represents a separate cron job. The syntax looks like this:
* * * * * [command or path to script]
The asterisks can have different values, determining when the cron job is to be executed. The five asterisks represent the minute, hour, day of the month, month, and day of the week, respectively. After that, you have the shell command or application to be executed.
For example, if we want the shell to output “hello world” every day at five minutes past midnight, we’ll have:
5 0 * * * echo hello world
If we want to have the same output at five past midnight on every fifteenth day of the month, we’ll have:
5 0 15 * * echo hello world
If we want it to happen every year on the fifteenth of April at five past midnight, we’ll have:
5 0 15 4 * echo hello world
Cron jobs on a web server are just as important. Often, they’re created manually by site owners who need to automate tasks related to their projects’ performance.
But how does WordPress handle them?
WordPress and Cron Jobs
The smooth operation of a WordPress project relies on automated tasks. For example, if you want to keep your CMS, themes, and plugins up-to-date, WordPress must periodically check for new versions. Scheduling posts is also a widely used feature that relies on automation. However, by default, WordPress doesn’t use cron jobs for several reasons.
For one, although it’s rare, WordPress can run on Windows, in which case, any attempts to use cron would be unsuccessful. And even if we look at Linux only, we’ll see that the cron configurations vary wildly from server to server.
That’s why WordPress instead relies on the wp-cron.php file to schedule and automate tasks. Although it has the word “cron” in its name, the concept is different.
Instead of keeping track of time and running commands according to a pre-selected schedule, wp-cron.php is loaded every time a user visits the website. If any tasks can be executed, wp-cron carries on with them automatically. Theoretically, it seems like a viable alternative, but in reality, it has a few inherent problems.
For one, the execution of tasks may not be as reliable as you’d wish. The wp-cron.php file is only loaded when there’s an active HTTP request to the site, so if there’s no traffic, it may fail to work as expected.
Furthermore, wp-cron.php is yet another file that the server needs to load every time a visitor access your site. This increases the load and consumes bandwidth.
It’s not uncommon for hackers to target the cron file either, and it could cause conflicts with some caching plugins.
Linux’s cron system is definitely more reliable, and it can help you improve the performance and reliability of your WordPress website. Before you can use it to automate your tasks, however, it’s a good idea to disable wp-cron.php.
Disabling wp-cron.php
A simple modification of the wp-config.php file can disable the default WordPress automated task behavior. You can use an FTP client, SSH, or your web hosting control panel’s File Manager to access wp-config.php in your site’s document root.
As we’re talking about the primary WordPress configuration file, it might not be a good idea to create a backup copy of it first, just in case.
Open the file and add the following above the line that says “/* That’s all, stop editing! Happy publishing. */“:
define(‘DISABLE_WP_CRON’, true);
Make sure you save the changes and re-upload the file if you’re using FTP.
Creating a Regular Cron Job
Disabling wp-cron may help you optimize your website’s performance and resource usage, but it will also disable the automated jobs that the PHP file takes care of by default. For them, you’ll need regular Linux cron jobs.
There are a couple of ways of setting up a cron job. If you prefer the command line, you can use the crontab -e command and save a new cron using the syntax we discussed a couple of sections ago. However, an easier option would be to use the utility in your web hosting control panel.
All popular management platforms have a graphical interface for setting up cron jobs. They may differ slightly in terms of design, but they should all offer the options you see in the screenshot below.
This is what SPanel users see when they open the Cron Jobs tool on the home page of the User Interface. You can determine when a cron job should be executed by entering values in the Minutes, Hours, Date, Month, and Weekday fields, or you can use the Common Settings in the drop-down menus.
The Cron command field determines what the cron job is going to do. You can enter a regular command or do what most developers prefer and put several tasks into a single script.
This does require diving deeper into the world of programming, but the internet is overflowing with resources, and you can also borrow ready-made scripts that take care of various site maintenance tasks. For example, with just three WP-CLI commands and four lines of code, you can have WordPress automatically check for and install new versions of the core, plugins, and themes.
This is what the script should look like:
Bear in mind that this is a very simple script. A bit more research will show you that you can easily broaden the range of tasks you do with a single file and a cron job. When you’re ready with the script, enter its path into the Cron command field to set it up to work with a cron job.