

Understanding WordPress Cron and a Reliable Alternative
How WordPress Handles Scheduled Tasks Unlike traditional cron jobs, WordPress manages scheduled background tasks in a unique way. Instead of running at precise intervals, WordPress checks for pending tasks on every page load. If a scheduled task is due, it executes during that request. Potential Issues with WP-Cron While this…Cookie Notice
This site utilizes cookies to improve your browsing experience, analyze the type of traffic we receive, and serve up proper content for you. If you wish to continue browsing, you must agree to allow us to set these cookies. If not, please visit another website.
Understanding WordPress Cron and a Reliable Alternative
How WordPress Handles Scheduled Tasks
Unlike traditional cron jobs, WordPress manages scheduled background tasks in a unique way. Instead of running at precise intervals, WordPress checks for pending tasks on every page load. If a scheduled task is due, it executes during that request.
Potential Issues with WP-Cron
While this system works for many sites, it has some drawbacks:
- Low-traffic sites may experience delays since tasks only run when someone visits.
- Irregular traffic patterns (e.g., few visitors overnight) can cause missed schedules.
- Coding errors in plugins/themes may prevent tasks from running properly.
Why Use WP-Cron?
Despite its quirks, WordPress relies on this system because:
- Shared hosting limitations: Many hosts restrict access to the system cron.
- Simplicity: The WordPress API is easier than configuring external cron jobs.
- Automatic retries: If a task misses its scheduled time, WP-Cron ensures it runs on the next page load.
The Problem: Unreliable Execution
Since WP-Cron depends on traffic, you can’t guarantee exact execution times. For mission-critical tasks, this uncertainty can be problematic.
A Better Solution: System Cron + WP-CLI
Instead of relying solely on WP-Cron, I wrote a bash script that triggers pending tasks across all WordPress sites on a server. By running this via the system cron every 10-15 minutes, you ensure tasks execute on time, regardless of traffic.
#!/usr/bin/env bash # hold the primary WP CLI command WP_CLI="wp --allow-root --path=" # hold the starting path _PATH=/home/*/htdocs/* # loop over our home directory for the htdocs folder... for d in $_PATH; do # now hold the formatted command CMD=$WP_CLI$d/; # check if this site is indeed a wordpress website if $CMD core is-installed 2>/dev/null; then # check if the site is a network if $CMD core is-installed --network 2>/dev/null; then # get the networks list of uri's _uris=$( $CMD site list --field=url ) # loop over the sites for _uri in $_uris; do # fire off the sites cron $CMD cron event run --all --due-now --url="$_uri" & done; # otherwise, we aren't in a network else # get the site's uri _uri=$( $CMD option get siteurl ); # fire off the sites cron $CMD cron event run --all --due-now --url="$_uri" & fi; fi; done;
How It Works
- Scans all WordPress installations in the specified path.
- Checks for multisite networks and processes each subsite.
- Runs all due cron tasks for every site, ensuring timely execution.
Setup Instructions
- Save the script (e.g., wp-cron-runner.sh).
- Make it executable:
chmod +x wp-cron-runner.sh
- Add a system cron job (e.g., every 15 minutes):
*/15 * * * * /path/to/wp-cron-runner.sh
Now, your WordPress tasks will run reliably, even during low-traffic periods.