/*Reverse sticky header*/

Server Cron Jobs for BuddyBoss

Introduction
WP-Cron is the scheduling system used by WordPress to automate time-based tasks such as checking for updates, publishing scheduled posts, and running maintenance tasks. However, WP-Cron executes on every page load, which can slow down your site if long-running processes are triggered. This guide explains how to optimize WP-Cron by disabling the default behavior and setting up a real cron job on your server, ensuring that scheduled tasks are executed efficiently without impacting page load times.

Why Use WP-Cron?

WordPress relies on WP-Cron for scheduling tasks because many hosting environments, especially shared hosting, do not provide direct access to the system scheduler. Here’s why WP-Cron is beneficial:

  • Simplicity: Using the WordPress API for scheduling is easier than configuring a system scheduler.
  • Reliability: Unlike system schedulers, WP-Cron queues all scheduled tasks. If a task is missed (e.g., due to site downtime), it will run at the next opportunity, ensuring that tasks are not skipped.
  • Compatibility: WP-Cron is compatible with all hosting environments, including shared hosting where direct access to crontab is not available.

Why Switch to a Real Cron Job?

By default, WP-Cron is triggered on every page load. This approach works for low-traffic sites but can cause performance issues on high-traffic sites due to:

  • Page Load Delays: Long-running tasks triggered by WP-Cron can delay page loading for visitors.
  • Inconsistent Scheduling: WP-Cron depends on site visits. If a site has low traffic, scheduled tasks may be delayed or not run at all.

To overcome these limitations, it is recommended to run WP-Cron via the system scheduler (Linux crontab) instead of the default WordPress method. This approach ensures that:

  • WP-Cron runs at fixed intervals regardless of site traffic.
  • Scheduled tasks are processed by an independent PHP process, preventing interference with page requests.

Disabling WP-Cron in WordPress

To switch to a real cron job, first, disable the default WP-Cron behavior by editing the wp-config.php file:

phpCopyEditdefine('DISABLE_WP_CRON', true);

This prevents WP-Cron from being triggered on every page load.

Setting Up a Real Cron Job on Your Server

Step 1: Open Crontab
From your Linux terminal, open the crontab editor using:

bashCopyEditcrontab -e

Step 2: Add the Cron Job
Choose one of the following commands depending on your setup:

  1. Using PHP-FPM or PHP-CGI
bashCopyEdit*/5 * * * * curl http://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
  • This runs WP-Cron every 5 minutes using curl.
  • Replace http://example.com with your site’s URL.
  1. Using PHP-CLI
bashCopyEdit*/5 * * * * cd /var/www/example.com/htdocs; php /var/www/example.com/htdocs/wp-cron.php?doing_wp_cron > /dev/null 2>&1
  • This method uses PHP-CLI, which has no time limits, making it suitable for long-running scripts.
  • Update the path to match your server’s directory structure.
  1. Using WP-CLI
bashCopyEdit*/5 * * * * cd /var/www/example.com/htdocs; wp cron event run --due-now > /dev/null 2>&1
  • This approach uses WP-CLI to run all scheduled cron events that are due.
  • Ensure WP-CLI is installed and configured on your server.

Step 3: Adjust the Frequency

  • The above examples run WP-Cron every 5 minutes (*/5).
  • To run it every 10 minutes, change */5 to */10.

Cron Syntax Overview

A cron job syntax consists of five fields followed by the command to execute:

sqlCopyEdit┌───────────── Minute (0 - 59)
│ ┌───────────── Hour (0 - 23)
│ │ ┌───────────── Day of month (1 - 31)
│ │ │ ┌───────────── Month (1 - 12)
│ │ │ │ ┌───────────── Day of week (0 - 7) (Sunday is 0 or 7)
│ │ │ │ │
│ │ │ │ │
* * * * * command to execute

Example:

bashCopyEdit*/5 * * * * curl http://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
  • This example runs the cron job every 5 minutes, regardless of the day or hour.

Note on Hosting Providers

If you do not have SSH access to your server, you can:

  • Request your hosting provider to set up the cron job on your behalf.
  • Use your host’s control panel to set up the cron job (most managed hosting platforms like Cloudways provide an interface for this).
Was this article helpful?

Related Articles

To speak to our Agency consultant, fill in the form found at our Contact Page.

"*" indicates required fields

Get Started

Enter your name and email address to get started with your project...

Name*