3

WordPress takes measures to ensure that a cron task doesn't run twice when it should run once, e.g. once every hour when an hourly schedule is given, rather than the occasional twice at the scheduled time.

While it's not foolproof, what steps does WordPress take to make sure it happens at the scheduled times, and only once as specified?

4

A transient is set to reflect the state of the cron, which gets deleted when all tasks complete. Any subsequent requests are ignored if that transient is found...

  • how reliable is this in practice? – Tom J Nowell Dec 3 '12 at 17:41
  • With WP's default object caching mechanism, it's as reliable as any other operation that relies on the database – onetrickpony Dec 3 '12 at 17:58
  • Isn't there a race condition with code like this? $lock = get_transient( 'lock' ); if ( $lock === false ) { set_transient( 'lock', 'locked' ); }; //etc – Flimm Mar 18 at 18:31
2

onetrickpony's answer is mostly correct. If you let WordPress do its default thing, or you manually trigger wp-cron by fetching its URL with 'doing_wp_cron' set to a unique value in the GET string, then you can reliably expect jobs to not be executed twice.

But if you execute wp-cron.php from the PHP CLI in your crontab, the story is different. Now it will check a constant called WP_CRON_LOCK_TIMEOUT to find out if it should ignore the lock that is stored in the transient. And there is a race condition that enables the same job to sometimes run twice.

If you want to be really sure the same job never executes twice, either don't set DISABLE_WP_CRON or use curl or wget in your crontab to fetch wp-cron.php like so:

*/10 * * * * wget -q -O "http://www.example.com/wp-cron.php?doing_wp_cron=`date +\%s.\%N`" > /dev/null 2>&1

Also note that there was a bug in 3.3 that caused jobs to be run twice if two instances of wp-cron.php were launched in the same second. Now they would need to be launched in the same microsecond, which is still possible, but highly unlikely.

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.