Wordpress' cron jobs seemed to cause some issues on our server, so I added this to the bottom of our wp-config.php:
define('DISABLE_WP_CRON', 'true');
And added this using 'crontab -e':
*/15 * * * * /usr/bin/php /path/to/wordpress/wp-cron.php > /var/log/cronoutput.log
If I'm not wrong, the above will have our server execute the Wordpress cron jobs every 15 minutes.
To test I added a cronjob in the functions.php theme file that simply sent me an email. I added the cronjob with this code in functions.php:
function my_cron_schedules($schedules){
if(!isset($schedules["5min"])){
$schedules["5min"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes'));
}
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_five_minutes' ) ) {
wp_schedule_event( time(), '5min', 'isa_add_every_five_minutes' );
}
// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_five_minutes', 'isa_add_every_five_minutes_func' );
function isa_add_every_five_minutes_func() {
mail( '[email protected]', 'this is a cron job', '' );
}
Then, I waited for the emails to arrive.
What I would have expected: an email once every 15 minutes, because the cron jobs should only execute once every 15 minutes, right?
But what happened was that:
- The email was sent about once every 5 minutes, as configured in the code, even though the cron job on the server should fire once every 15 minutes.
- Usually only a single email would be sent at once, as expected, but sometimes over a dozen emails would be sent at once.
So my questions are: how come the cron jobs are still being executed every 5 minutes, even though the wp-cron.php file should only be executed once every 15 minutes?
And, how is it possible that the cron job sometimes is executed over a dozen times in one minute?