Client Area

 Client Area

Improve WordPress Performance by running wp-cron with a linux cronjob

wp-cron

Sections

    WordPress use wp-cron to check if there are new updates available, but also to perform scheduled tasks added by plugins. But it can impact your website performance because WordPress execute wp-cron on every page-loading by default. So, it’s a good solution for website with high-traffic to disable wp-cron, and to run them with a linux cronjob.

    Disable wp-cron

    To disable wp-cron, you just have to add the following line inside your wp-config.php file :

    define( 'DISABLE_WP_CRON',      'true' );

    Additional Settings

    You can also add the following line to avoid issues with posts scheduling feature of WordPress :

    define( 'ALTERNATE_WP_CRON',    'true' );

    The last possible option, is to set a timeout for wp-cron to not impact website performance if a wp-cron take too long :

    define( 'WP_CRON_LOCK_TIMEOUT', 60 );

    Setup a linux cronjob

    To run wp-cron using a linux cronjob, you have to execute the file wp-cron.php located in WordPress root directory.
    To add a new cronjob the command is :

    crontab -u www-data -e

    There are two way to execute wp-cron :

    Run wp-cron with http requests to wp-cron.php

    By making an HTTP request to wp-cron.php, wp-cron will be processed by php-fpm file like this :

    */10 * * * * curl http://yourwebsite.tld/wp-cron.php?doing_wp_cron > /dev/null 2>&1

    Run wp-cron with the php-cli

    */10 * * * * cd /var/www/votresite.tld/htdocs; php /var/www/yourwebsite.tld/htdocs/wp-cron.php > /dev/null 2>&1

    In this example, wp-cron will be executed with 10 minutes interval, you can set a shorter interval by replacing */10 by anoter value, like 5 minutes with */5 .

    Tutorial based on the  article Better wp-cron using linux’s crontab.

    in WordPress