RunCloud makes setting up and running cron jobs from your dashboard simple without messing with server settings. (Refer to our previous post to learn why cron jobs are necessary for WordPress.)
Adding a Cron Job
There are multiple ways to add a cron job to RunCloud. However, we recommend using WP-CLI to run cron jobs as it provides greater flexibility.
Method 1: Using WP CLI (Recommended)
Follow the steps below to schedule cron jobs for both single-site and multisite WordPress installations on RunCloud:
First, you need to log in to your RunCloud dashboard and copy the root path of your web application.
Next, navigate to the server page and click “CronJob” in the left menu.
Click “Add New Job” to create a new job.
On the next screen, you can specify the details about the job. Provide a descriptive name, and in the user field, enter the name of the web application’s owner.
In the “Vendor Binary” drop-down, select (Write your own, inside command). The following steps are slightly different depending on your WordPress environment. Pick the option that suits you.
Cron Jobs for Simple WordPress Installation
If you are running a simple WordPress installation, then you can simply copy and paste the following code snippet in the provided text field and replace the /path/to/wordpress/root
with your web application’s root path that you copied earlier:
wp cron event run --path="/path/to/wordpress/root" --due-now
The above command will execute the WordPress cron scheduler and execute all the tasks that have been overdue since the last time this command was executed. To ensure that the overdue tasks are completed regularly, you should set the job frequency to 5 minutes using the drop-down menu. Finally, click on “Create Cron Job” to save it.
Cron Jobs for Multisite WordPress Installations
The other methods for scheduling cron jobs in WordPress multisite networks require manually adding each site’s URL individually, which can be time-consuming and requires frequent updates when sites are added or removed.
RunCloud experts have developed a more efficient command that automatically retrieves and executes cron jobs for all active subsites in your network to streamline this process. To learn more about this topic, read our blog post, Pipes vs Xargs: Which One To Use When Writing Bash Scripts In Linux.
To implement this improved method for WordPress multisite installations, follow the steps described above and enter the following command in the provided text box:
wp_path="/path/to/wordpress/root" ; wp site list --field=url --deleted=0 --archived=0 --path=$wp_path | xargs -I {} wp cron event run --due-now --url="{}" --quiet --path=$wp_path
After pasting the above command, you need to replace “/path/to/wordpress/root” with your WordPress multisite network’s root path that you copied earlier:
Finally, use the drop-down menu to set the job frequency to 5 minutes and click “Create Cron Job” to finalize. This method ensures that cron events run for all active sites in your multisite network without requiring manual updates when adding or removing sites.
Method 2: By Executing wp-cron.php
Note: This method will not work for WordPress multisite installations.
To add a cron job to a WordPress application on RunCloud, open the web application dashboard and note the application’s owner and the PHP version.
Next, copy the website’s public path to your clipboard. This is where the wp-cron.php file is stored.
After you have copied the path, open the “Cronjob” menu and click on the “Add New Job” button to create a new entry.
On the next screen, you can configure the job. First, provide a descriptive name to the job, and then specify the user account that will run this job on your server (owner of the application).
Next, specify the PHP version of your application in the vendor binary drop-down.
After that, you need to specify the path to cron file – simply paste the path to the public directory that we copied before, and then add /wp-cron.php after it.
Finally, you need to specify how often you want to execute this cron job. For most WordPress websites, a frequency of every 10 minutes is good enough.
Disabling WordPress’s Wp-Cron
After manually setting up the cron job on your server, you need to explicitly disable the WordPress’s cron execution. You can do this without leaving the RunCloud dashboard.
Open the “File Manager” page for your WordPress site and look for wp-config.php. Click on it to open a text editor in a new window.
Look for the line that says ‘Add any custom values between this line and the “stop editing” line.‘ and paste the following code below it:
define( 'DISABLE_WP_CRON', true );
Once you save the changes, WordPress will stop executing the default cron job on page loads.
Verifying Cron Schedule
There are several ways to verify that your cron jobs are executed correctly in your WordPress installation. But before you can do that, you should wait a couple of hours and provide the cron job some time to execute its scheduled events.
Using WordPress CLI
One of the most straightforward methods is to use the WordPress Command Line Interface (WP-CLI). Navigate to the root directory of your multisite installation and run the following command:
wp cron event list
This command will display a list of all scheduled cron events, their next run time, and their recurrence. It provides a quick overview of what’s scheduled and when it’s set to run next.
In the above example, we can see that no events are due now and that they have missed their schedule. Therefore, we can rest assured that the cron job is working correctly.
Checking System Logs
You can also verify cron job execution by examining the Linux system logs. Log in to your server with administrator privileges and execute the provided command to see the log entries corresponding to your cron jobs.
The following command will search the system log for entries related to your provided keyword (name of your site) and show you the last ten entries.
grep -i "site-name" /var/log/syslog | tail
In the above example, replace the “site-name” with the name of your WordPress website (in the RunCloud dashboard).
The above example shows the date and time when the cron job was executed in the highlighted sections. This information can help you confirm if the job is being triggered at the scheduled times.