22

How can I debug problems with WordPress Cron? I think it will trigger when users go to your site but any errors wont be shown to them, as the jobs are run "asynchronously". So how might I debug errors?

I use wp schedule event

29

You can run WP cron manually by calling: http://example.com/wp-cron.php?doing_wp_cron

If you don't want the automatic cron to run while you're debugging, then add this to your /wp-config.php file:

define('DISABLE_WP_CRON', true);

If you're on a development environment and want to output debug information, calling it manually like that will show you your debug output.

Alternatively you can use PHP's built-in error_log function to log message strings to the error log for debugging. You'd need to use this in conjunction with WP_DEBUG settings, as mentioned by Rarst.

|improve this answer|||||
  • Thank you for the hint with the ?doing_cron parameter. – rofflox Mar 31 '11 at 19:11
  • 3
    I believe it should be ?doing_wp_cron instead of ?doing_cron. – liviucmg Apr 23 '15 at 10:14
  • @liviucmg Yes, you're right. I've made the tweak. – Simon East Aug 4 '15 at 5:14
  • 1
    Is the ?doing_wp_cron parameter required? See EasyCron's manual set up tutorial. – AlecRust Sep 3 '16 at 11:25
  • @gabrielk Is the ?doing_cron parameter required? what does it mean? – jedi Feb 23 '17 at 9:31
6

You could use the plugin Cron-View. There you can see if your job is a) registered and b) what the next due time is.

In addition, you could add a lower schedule-timer to your event (e.g. every 2 min) and test your method more frequently on a local system. Use the 'cron_schedules' filter hook to register new schedule times. For example:

function my_additional_schedules($schedules) {
    // interval in seconds
    $schedules['every2min'] = array('interval' => 2*60, 'display' => 'Every two minutes');
    return $schedules;
}
add_filter('cron_schedules', 'my_additional_schedules');
|improve this answer|||||
3

You can (and probably should in any case, cron or not) configure PHP error log to capture all errors.

|improve this answer|||||
3

You could debug manually, by creating an action and executing the Cron action inside. Like this:

add_action( 'init', function() {

    if ( ! isset( $_GET['the_cron_test'] ) ) {
        return;
    }

    error_reporting( 1 );

    do_action( 'this_is_cron_event_hook' );

    die();

} );

And by going to your website's address: http://example.com?the_cron_test

This should show you any errors with the cron task.

But it's without any sense doing it manually. You could use Advanced Cron Manager PRO plugin which does this for you and also saves the log and other stats.

|improve this answer|||||

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.