The MU forums have moved to

Cron twice daily, runs once if not tripped? (10 posts)

  1. delayedinsanity
    Posted 14 years ago #

    I'm delving into the world of using wp-cron for a plugin that I'm developing at the moment, and was just curious if somebody could answer a question...

    I wasn't able to determine this from the codex, nor track it down in the code yet; if you schedule an event to run twicedaily, what happens if the first occurrence of the event is never tripped?

    Since wp-cron is only ran once a visitor arrives at your site after the scheduled event is set to occur, and I'm assuming (and hoping in a way) that a visit to Blog A won't trip events for Blog B, I'm curious if Blog C has no visits for a 12 hour period of time, will it attempt to run the event twice, or is it smart enough to only run it once?

    I'm considering getting creative and writing a shell script that will attempt to trip the wp-cron event a moment after it's set to run, but if this plugin winds up making its way to public release, I just want to be sure of what will happen.

  2. tim.moore
    Posted 14 years ago #

    I use wp-cron (via a plugin called WP-Crontrol) to schedule custom events. As far as I know, it runs on a time-based schedule and does not depend on a page visit as a trigger. You might look into WP-Crontrol and see if that helps at all.

  3. SteveAtty
    Posted 14 years ago #

    wp-cron does need a site visit for it wake up though, I'm playing round with it and I've got it doing something on two blogs. One one which is visited a lot my process runs pretty much every hour, on the other site (which is not listed on search engines) it only fires when I go and look at the site.

  4. delayedinsanity
    Posted 14 years ago #

    As far as I know, it runs on a time-based schedule and does not depend on a page visit as a trigger.

    Since it doesn't (and couldn't effectively) hook into your server's cron (or whatever IIS calls it if that be your poison), it requires somebody (or something) to physically wake up your web server and trigger a scheduled event. No page view, means no event. :(

    It'd be pretty snazzy if some kind of module or extension could be developed that allowed web servers such as apache to wake themselves up and fire off a seperate process, so as to not directly affect your visitors, but afaik, no such module exists.

    In the meantime, I guess I'll just have to keep backtracing the code until I can find out if a check has been added to make sure an event doesn't run twice if not previously triggered.

  5. tmoorewp
    Posted 14 years ago #

    Ah, ok. I'll have to go review what I did since I wasn't aware that a page view was necessary. Might have some unhappy users on my hands...

  6. delayedinsanity
    Posted 14 years ago #

    I'm going to run a test today, by scheduling an event to drop an entry into the database on an empty site. I'll access it once to make sure the event gets scheduled, then let it sit for 14+ hours and access it again to see if the event runs twice at that point or just once.

    If it's built in to catch this, great, if not I'm sure a workaround won't be very complicated at all.

  7. SteveAtty
    Posted 14 years ago #

    Here's an extract from my /tmp directory where a wp_cron job is writing debug info:

    -rw-r--r-- 1 www-data www-data  42109 2009-10-21 13:37 wordbook_wp_8_debug
    -rw-r--r-- 1 www-data www-data 247996 2009-10-22 16:02 php_errors
    -rw-r--r-- 1 www-data www-data  39412 2009-10-22 22:07 wordbook_wp_3_debug

    WP_8 is my test blog which is not hit by google or normal users. WP_3 is my blog which is accessed by google and end users.

    So the WP_8 cron job has not fired since 13:37 yesterday, and according to wp_cron its next scheduled time is : 22:36:34

    Thats on an hourly schedule so it will next fire then but has not run since yesterday.

  8. delayedinsanity
    Posted 14 years ago #

    Looks like that it skips events on the same hook that aren't triggered. I'm still going to run my own test to double check and confirm, which actually would have been completed already had it not been for wp-crontrol.

    Not sure what's wrong with that plugin, other than multiple PHP errors on it's administration page, but it managed to intercept my scheduled test event. WP-Cron wouldn't schedule any new events until I deactivated Crontrol, which I just now discovered. I'm a moron and didn't check my options table two hours ago when I fired up my test plugin to confirm it hooked. Ah, Murphy and time, my old and faithful enemies.

  9. delayedinsanity
    Posted 14 years ago #

    Alrighty, confirmed.

    Any event that isn't triggered will only run once after the most recent specified interval. :) Kind of figured they would have us covered on that one. Just had to be sure.

  10. dreamdevil
    Posted 14 years ago #


    I am a little late... but it's because I just got my hands into the scheduled tasks mecanism of Wordpress and wanted to share some additional information.

    First, I double confirm delayedinsanity (from code perspective). In fact, wordpress keeps an array of next tasks to run. When it runs a recurring task, it recompute the next schedule as of now and store it into the array. The array is persisted into wp_xx_options. That means 2 things:
    1- If you didn't receive web requests between schedules, it will skip those in between.
    2- In Wordpress MU, requests to a blog won't execute scheduled tasks for another blog (wp_xx_options is per blog table)...

    Second thing that may be of interest is I wrote an article and instructions to run tasks from cron job if hosting allows it:
    Run Wordpress tasks from real cron job


About this Topic

  • Started 14 years ago by delayedinsanity
  • Latest reply from dreamdevil