Skip to content

Instantly share code, notes, and snippets.

@derak-kilgo
Last active August 29, 2015 13:58
Show Gist options
  • Save derak-kilgo/10425518 to your computer and use it in GitHub Desktop.
Save derak-kilgo/10425518 to your computer and use it in GitHub Desktop.
multisite cron for wordpress
<?php
/**
* Multisite Cron task. Hits the cron url for all enabled blogs so scheduled tasks function designed.
* @see http://www.lucasrolff.com/wordpress/why-wp-cron-sucks/
*
* Add this to your /etc/crontab file. Runs once per hour. Outputs log.
* 1 * * * * wget -q -O - "http://mydomain.com/wp-cron-multisite.php?doing_wp_cron >> /tmp/wp-cron.log
*/
require('./wp-load.php');
//increase memory for this process.
ini_set('memory_limit','512M');
global $wpdb;
//fetch list of active blogs
$sql = $wpdb->prepare("SELECT domain, path FROM $wpdb->blogs WHERE archived = '0' and deleted = 0", '');
$blogs = $wpdb->get_results($sql);
foreach($blogs as $blog) {
set_time_limit(180); //3min per site max.
$command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php?doing_wp_cron';
//only run if load is low.
$load = sys_getloadavg();
if ($load[0] <= 1.0) {
echo 'Running cron for ' . $blog->path . date('Y-m-d h:i:s') ."\n";
$ch = curl_init($command);
$rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
$rc = curl_exec($ch);
curl_close($ch);
}else{
echo 'Too busy. Skipped ' . $blog->path . date('Y-m-d h:i:s') . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment