Support » Developing with WordPress » wp cron is not running my function

  • Resolved Holkan

    (@cubeinthebox)


    Hello there!

    What I’m trying to achieve is to run a function every five minutes with a cron job. I’ve followed the docs about wp cron here. Everything seems to be all right, my recently created hook is on the list of cron jobs, but the thing is that my function never executes and I don’t know how to debug why is it happening.

    When the function is attached to some other hook like save_post , it does executes and the expected result is achieved, so I think I can discard any errors in my function.

    Please, any advice on how to debug this will be appreciated.

    Thanks in advance!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The WP cron is triggered by site visits. It won’t run unless someone visits a WP URL. If you need it to run on a fixed schedule, use a real cron on your server or one of the many external cron services.

    Thread Starter Holkan

    (@cubeinthebox)

    Hi @joyously

    Thank you for your response. Yes, I know that, actually I’ve disabled the WP cron due to my site having thousands of visits per hour, so instead I have a real cron that executes wp-cron.php every minute. Something like this:
    * * * * * curl -vs -o -S https://mysite.com/wp-cron.php?doing_wp_cron > /home/mysite/logs/cronwp.log 2>&1.

    So, is it in the way that you disabled WP cron? How can it be disabled, yet your cron job calling it should work?

    Thread Starter Holkan

    (@cubeinthebox)

    Sorry i forgot to mention. The wp cron can be disabled adding this line to wp-config.php file: define('DISABLE_WP_CRON', true);.

    AFAIK, by calling wp-cron with a real cron is just to ensure that the actual tasks get executed at a regular interval.

    When I check for the jobs added to the cron with wp cron event list I can see that my task is added to the queue.

    Even I can execute it by running wp cron event run custom_cron_hook and the output is

    Success: Executed 1 task …

    but still I don’t see that the function that should be called gets called, nor the file that should be created.

    These are the relevant docs: https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/

    I’m pretty lost here, please be patient :\

    @cubeinthebox Did you make any progress on this? I’m stuck at a similar place. The event is properly scheduled. But when it’s executed, nothing happens (or rather, the hook is never called). No errors in any log file, no complaints from any code. It’s just dead silent.

    Thread Starter Holkan

    (@cubeinthebox)

    hi there @joho68
    Unfortunately this didn’t work, so I ended up using a regular cron job on my server. I moved all the logic outside WordPress and just call the required the necessary core files to be able to call wp functions in my script. After that I only registered a new cron job with crontab and that was that!

    Thanks for getting back to me @cubeinthebox. I cannot for the life of me figure out what’s going on. WordPress goes through all the motions, but nothing happens. When the time is up for my scheduled event to be executed, WordPress renews the timer (“hourly”) properly, but the hooked function is not called, nor are any error messages logged.

    Using the WP Cron Pixie plugin, I can see all the events/schedules, and everything looks as it should. I tried placing the hook to be executed outside of a class and just make it a global function, same result 🙁

    pablolopezmestre

    (@pablolopezmestre)

    I have exactly the same issue. Cron-job does not run automatically

    – I tested with and without defining DISABLE_WP_CRON
    – My event appears when I use ‘wp cron event list’
    – My event runs when I use ‘wp cron event run my_event’
    – I tried to enter in ‘https://mysite.com/wp-cron.php’ and nothing happens. Tested with ‘?doing_wp_cron’ and ‘?doing_wp_cron=1’ and nothing happens too.
    – I put error_logs in ‘wp_cron’ functions… only write in there when I run my event trought CLI
    – There is no HTTP Auth user/pass
    – In local environment it works perfectly
    – I installed WP Crontrol plugin. My event appears, but when I click in ‘run now’, nothing happens.
    – I tested deactivating all plugins…. It does not work anyway

    Any ideas?

    ermWP

    (@ermwp)

    I have this same issue. I have disabled the wp-cron via the wp-config.php file. My cron job on my server calls the wp-cron.php, but none of my hooks are called.

    EDIT:
    My hooks are called, but “echo” statements and things sent to “php://output” are not seen on the screen when wp-cron.php is called manually via the browser nor are they sent to the log. I had to set up an SMS integration to verify that wp-cron.php was indeed executing the hooks.

    • This reply was modified 3 years ago by ermWP.

    I have the same issue.

    • I’ve deactivated the default cron spawning by setting DISABLE_WP_CRON to true
    • I’ve made a cron job on the host which calls wp_cron.php?doing_wp_cron every minute.
    • The event appears in the cron list using the WP Crontrol plugin, and apparently runs. But there is no output, either to error log or saving an option.
    • I’m able to run the job via WP-CLI, which reports that it has successfully run. But again, the action handler hasn’t done anything.

    I’m able to make this work, so maybe it’s something to do with the way I’m adding the action handler.

    
    add_action( 'init', function () {
    	if ( ! wp_next_scheduled( 'do_single_action' ) ) {
    		wp_schedule_single_event( time(), 'do_single_action' );
    	}
    
    	add_action( 'do_single_action', 'do_this_once' );
    
    	function do_this_once() {
    		error_log( 'DO THIS ONCE' );
    	}
    } );
    
    • This reply was modified 2 years, 11 months ago by andfinally.
    • This reply was modified 2 years, 11 months ago by andfinally.

    In my case I was able to get it working by jiggling things round a bit. I added an init action at the root level of a file which is included by functions.php:

    
    add_action( 'init', function () {
    	if ( ! has_action( 'import_scheduled_job' ) ) {
    		add_action( 'import_scheduled_job', 'Products\do_import', 10, 2 );
    	}
    } );
    

    And added the action handler at the root level of a file which contained my import class, which calls the class’s import method.

    
    namespace Products;
    
    class Product_Importer {
    
      // Other functions, including one which calls create_async_job 
      // when the user starts an import
    
      private function create_async_job( $file ) {
        if ( ! wp_next_scheduled( 'import_scheduled_job' ) ) {
          wp_schedule_single_event(
            time(),
            'import_scheduled_job',
            array(
              'file' => $file,
              'attachment_id' => $this->id
    	)
          );
        }
      }
    
      public static function import( $file, $attachment_id ) {
        // ...
      }
    }
    
    function do_import( $file, $attachment_id ) {
    	Product_Importer::import( $file, $attachment_id );
    }
    

    Not very elegant, but it does the job for now. Hope this helps other people with the same problem. I’m still not sure why I was getting this, but judging by the fix, I suspect it’s something to do with the scope of classes and/or namespaces.

    One gotcha that might catch other people: you pass an $args array of parameters when you call wp_schedule_single_event, but when the action handler is called, those params are passed individually. So if your array has two elements, specify 2 arguments in your add_action call.

    • This reply was modified 2 years, 11 months ago by andfinally.
    Thread Starter Holkan

    (@cubeinthebox)

    @andfinally This looks like a solution. I’ll give it a try Thanks!!

    @andfinally

    So glad to hear that there’s a fix!! I’ve been having a nightmare with this!

    WP newb here- would it be possible to run that code in 2 separate snippets in the code snippets plugin as a fix?

    –Still getting to grips with WP

    Hi @lauraabraham, I’m not too familiar with the Code Snippets plugin, but it looks like it should work just as well as if you’d added the code to your functions.php. You might even be able to combine them in one snippet.

    After long searching the solution of @andfinally got me on the right track. It was the hint about the namespacing, though it’s not a namespacing problem. But it got me thinking about instances 🙂

    What happens:
    -you go to a page
    -the php script runs and creates instances of classes
    -in add_action when you add your callback something like this:
    add_action( ‘process_something’, [$this, ‘process_something’], 10, 3 );
    -and then you call wp_schedule_single_event with this action
    -then the script finishes

    Then later the WP Cron is executed. It’s a total different run of the script. The instance that was registered with the callback does not exist, so it can never be called.

    So the add_action with the callback should indeed be registered in for example the init hook. Or anything that is also executed / instantiated when wp-cron runs. And then it can be perfectly fine in an instance of a namespaced class

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘wp cron is not running my function’ is closed to new replies.