generalwordpress-multisite

Disable scheduled tasks

The wp-cron.php script will run once a user visits your site. If you get a lot of traffic, this could be a problem. This cron task is really necessary when you make updates in the blog. You can move this cron script to a system cron task to help lower resource usage on the server.

Disable the wp-cron.php script in the WordPress /opt/bitnami/wordpress/wp-config.php configuration file. The location is important - add the line below just before the database settings:

define('DISABLE_WP_CRON', true);

Then, add the cron task to the system. There are two options:

  • Create a cron task for each WordPress site and set a different execution interval for each. For example, the cron tasks shown below will run every 15 minutes and 30 minutes respectively. This is fine if there aren’t a large number of sites.

      $ sudo crontab -e
      */15 * * * * wget -q -O - "http://our-planets.com/wp-cron.php?t=`date +\%s`" > /dev/null 2>&1
      */30 * * * * wget -q -O - "http://earth.com/wp-cron.php?t=`date +\%s`" > /dev/null 2>&1
    
  • Create a unique cron task for all the domains and share the execution interval. This option is better if you have a large number of WordPress sites and all of them share the same tasks.

    • Create the following script as /opt/bitnami/wordpress/wp-cron-multisite.php at the WordPress installation directory:

        <?php
        require(__DIR__ . '/wp-load.php');
        global $wpdb;
      
        $sql = $wpdb->prepare("SELECT domain, path FROM $wpdb->blogs WHERE archived='0' AND deleted ='0' LIMIT 0,300", '');
      
        $blogs = $wpdb->get_results($sql);
      
        foreach($blogs as $blog) {
      
            $command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php';
            $ch = curl_init($command);
            $rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
            $rc = curl_exec($ch);
            curl_close($ch);
        }
        ?>
      
    • Set the correct permissions:

        $ sudo chown bitnami:daemon wp-cron-multisite.php
      
    • Add the task to the system crontab. For example, this cron task will run the script for each domain every hour. Add it using the following command:

        $ sudo crontab -e
        0 * * * * wget -q -O - "http://our-planets.com/wp-cron-multisite.php?t=`date +\%s`" > /dev/null 2>&1
      

For more information, refer to this blog post.

Last modification February 9, 2023