Technical guide on disabling the virtual WP-Cron and replacing it with a high-reliability Linux system cron job on Hovixa VPS to optimize performance and task execution.

Replacing WP-Cron with a System-Level Cron Job for Execution Reliability

By default, WordPress handles scheduled tasks (like publishing posts, checking for updates, or sending newsletters) using a "virtual" cron system called WP-Cron. This system only triggers when someone visits your website. On low-traffic sites, scheduled tasks may never run; on high-traffic sites, the constant checking can cause significant CPU overhead on your Hovixa VPS. Replacing this with a System Cron ensures tasks run on a precise schedule regardless of site traffic.

1. Why the Default WP-Cron is Flawed

WP-Cron is a "poor man's cron." Every time a page is loaded, WordPress spawns a background request to wp-cron.php. If no one visits your site for 5 hours, no tasks are executed during that window. Conversely, if 1,000 users visit in a minute, 1,000 checks are performed, wasting NVMe and CPU cycles.

2. Disabling the Default WP-Cron

The first step is to tell WordPress to stop attempting to run its own cron system. Add this constant to your wp-config.php file:

/** Disable virtual WP-Cron */
define( 'DISABLE_WP_CRON', true );
    

3. Configuring the System Cron (Crontab)

Now, we will use the Linux crontab to trigger the cron script at a fixed interval (e.g., every 5 minutes). This uses the PHP binary directly, which is faster and more reliable than a web-based trigger.

Execution Steps:

  1. Open the crontab for the web user (e.g., www-data):
    sudo crontab -u www-data -e
                
  2. Add the following line to the bottom of the file:
    */5 * * * * php /var/www/html/wp-cron.php > /dev/null 2>&1
                

4. Advanced: Using WP-CLI for Cron Execution

For even better performance, use WP-CLI to run the cron. This is the most efficient method as it uses the optimized CLI environment and provides better logging options.

*/5 * * * * /usr/local/bin/wp cron event run --due-now --path=/var/www/html > /dev/null 2>&1
    

5. Technical Comparison Table

Feature Standard WP-Cron System Cron (Linux)
Trigger User page visits Server clock (Fixed interval)
Precision Low (Variable) High (Exact)
CPU Impact Spiky / Inefficient Predictable / Optimized
Reliability Fails on low-traffic sites Always executes

6. Technical Implementation Details

  • Path Accuracy: Always use absolute paths in your crontab (e.g., /usr/bin/php instead of just php) to avoid environment variable issues.
  • Timeout Limits: System cron runs via the php-cli, which usually has no max_execution_time limit. This is beneficial for long-running tasks like large backups or email queue processing.
  • Logging: If you suspect a task is failing, redirect the output to a log file instead of /dev/null to debug: >> /var/log/wp-cron.log 2>&1.

Sysadmin Advice: After setting up the system cron, install the "WP Control" plugin. It allows you to see the exact time the next "hook" is scheduled for and verify that your system cron is correctly checking off those events.

War diese Antwort hilfreich? 0 Benutzer fanden dies hilfreich (0 Stimmen)