WordPress calling WP-Cron vs cron job calling WP-Cron

You probably already know WordPress uses a virtual cron to handle scheduled events. What you may not know is it can be either very imprecise or a waste of server resources.

Here’s why: WordPress forks a process called WP-Cron in the background every time a WordPress page is loaded to check for scheduled events that need to be run.


 
If you have a new or low traffic site, WordPress will only call WP-Cron when a page is loaded, which might not be until well after the scheduled event was supposed to occur.

WordPress calling WP-Cron on low traffic site

12:00 – You set a WordPress event to trigger every 15 minutes, starting at 12:15.
12:07 – A visitor to your site forks WP-Cron. Nothing to do.
12:38 – A visitor to your site forks WP-Cron. The event scheduled for 12:15 triggers and the next event is scheduled for 15 minutes at 12:53.
12:58 – A visitor to your site forks WP-Cron. The event scheduled for 12:53 triggers and the next event is scheduled for 15 minutes at 1:13.

If you need exact time for events to occur, using virtual WP-Cron is not the way to go.


 

If you have a high traffic site, hundreds of these forks could be created before a single event needs to be processed, so it can be highly inefficient.

WordPress calling WP-Cron on high traffic site

12:00 – You set a WordPress event to trigger every 15 minutes, starting at 12:15.
12:01 – 2 site visitors in the previous minute forks WP-Cron 2 times. Nothing to do.
12:02 – 5 site visitors in the previous minute forks WP-Cron 5 times. Nothing to do.
12:03 – 6 site visitors in the previous minute forks WP-Cron 6 times. Nothing to do.
12:04 – 2 site visitors in the previous minute forks WP-Cron 2 times. Nothing to do.
12:05 – 7 site visitors in the previous minute forks WP-Cron 7 times. Nothing to do.
12:06 – 5 site visitors in the previous minute forks WP-Cron 5 times. Nothing to do.
12:07 – 8 site visitors in the previous minute forks WP-Cron 8 times. Nothing to do.
12:08 – 2 site visitors in the previous minute forks WP-Cron 2 times. Nothing to do.
12:09 – 4 site visitors in the previous minute forks WP-Cron 4 times. Nothing to do.
12:10 – 3 site visitors in the previous minute forks WP-Cron 3 times. Nothing to do.
12:11 – 4 site visitors in the previous minute forks WP-Cron 4 times. Nothing to do.
12:12 – 2 site visitors in the previous minute forks WP-Cron 2 times. Nothing to do.
12:13 – 5 site visitors in the previous minute forks WP-Cron 5 times. Nothing to do.
12:14 – 7 site visitors in the previous minute forks WP-Cron 7 times. Nothing to do.
12:15 – 8 site visitors in the previous minute forks WP-Cron 8 times. Nothing to do.
12:15:00 – A visitor to your site forks WP-Cron. The event scheduled for 12:15 triggers and the next event is scheduled for 15 minutes at 12:30.

Lots of forks to WP-Cron to check for schedule events that only happen every 15 minutes.


 

You can disable the virtual cron on WordPress by adding the following line to your site’s wp-config.php file where your WordPress is installed:

define( 'DISABLE_WP_CRON', true );

Once the virtual cron is disabled, we need to replace it with a “real” server cron job that will fork WP-Cron for WordPress at specific intervals to check for scheduled events.

You may do this depending on your server access via the command line or through your host’s config panel software, but essentially you want to add this line to your cron file:

*/5 * * * * wget -q -O - "http://www.yourdomain.com/wp-cron.php?doing_wp_cron=date +\%s" > /dev/null2>&1

Change “yourdomain.com” of course. This example above now forks the wp-cron process on your site so WordPress can check for scheduled events every 5 minutes. Here’s the difference:

Server Cron job calling WP-Cron set to run every 5 minutes.

12:00 – You set a WordPress event to trigger every 15 minutes, starting at 12:15.
12:05 – Server cron job forks WP-Cron. Nothing to do.
12:10 – Server cron job forks WP-Cron. Nothing to do.
12:15 – Server cron job forks WP-Cron. The event scheduled for 12:15 triggers and the next event is scheduled for 15 minutes at 12:30.

A much better solution regardless of your site’s traffic.

 

John Holt

I’m a software engineer and web designer in Fort Pierce, Florida (winter) and Franklin, New Hampshire (summer). Super Blog Me is a space where I can brainstorm my ideas on WordPress development, interface design and other things I do with my life.

Leave a Reply

Your email address will not be published. Required fields are marked *