1

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( 'myemailaddress@gmail.com', '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:

  1. 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.
  2. 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?

1

You might be calling wp-cron.php every 15 minutes, but your cron task is set to happen every 5 minutes!

    $schedules["5min"] = array(
        'interval' => 5*60,
        'display' => __('Once every 5 minutes'));

...

wp_schedule_event( time(), '5min', 'isa_add_every_five_minutes' );

Change it instead to every 15 minutes

  • I was aware of that, but if I'm not mistaken, once you use 'define('DISABLE_WP_CRON', 'true')' you disable Wordpress' internal cron job execution, and you depend on the server's cron jobs. If those only fire once every 15 minutes, how can the code still run once every 5 minutes then? This can very well be a misunderstanding on my part, but at this point I'm still confused. – user2202070 Sep 6 '17 at 11:20
  • Are you sure that's the only machine? (WP Cron + load balancer = multiple machines running cron at the same time)That nothing else is pinging the URL? That it's not just queueing 3 tasks then running them all at once every 15 minutes? Have you tried with a more reliable method of testing such as a message in the error log? ( emails can take time to arrive and you're at the mercy of whatever mail server there is and how it operates ) – Tom J Nowell Sep 6 '17 at 13:27
  • And have you confirmed by manually running cron via WP CLI with the crontab turned off? And that it is indeed truly disabled by disabling the crontab and seeing if it still sends emails? There's an assumption here that's false, and we need to find it – Tom J Nowell Sep 6 '17 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.