Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Ensure WP Cron Runs On Time

Tags: cron wordpress

WP Cron is the schedule system implementation of WordPress. Unlike linux cron, it doesn’t guarantee jobs are performed on time. Ever wondered why your scheduled posts not posted at specific time but suddenly published when you log in to WordPress dashboard to check out what’s wrong? You are the victim of WP cron.

How does WP Cron Work?

Scheduled jobs are saved into database and wait for WP Cron to perform them. However, unlike traditional cron systems, it’s doesn’t watch the clock periodically. 

WP Cron only checks for due jobs when any WordPress page is loaded.See the problem?Click To Tweet

WP Cron Problems on Low Traffic Sites

On a low-traffic WordPress site, WP Cron often perform the scheduled jobs beyond their specific time because nobody visits WordPress. Thus, nobody triggers WP Cron to check for scheduled jobs. Finally, someone visit WordPress and fires too many jobs, causing PHP timeouts.

WP Cron Problems on High Traffic Sites

On the other hand, high-traffic sites trigger WP Cron too often. As jobs are saved in database, querying for cron jobs become a waste of resource. Worse still, multiple WP Cron instances might performing the same job at the same time. This could be problem if the jobs are not idempotent (safely execute multiple times). Lucky for you, WordPress has a better locking system and much less issues about this is reported since version 3.

Solution to WP Cron

To prevent WP Cron not fired or triggered too often, we can use a real cron job to execute it.

Disable WP Cron

First, disable the default WP Cron behaviour. Add this line in wp-config.php.


define( 'DISABLE_WP_CRON',  true );

So no WP Cron is performed when page loads.

Using Linux Crontab

SSH to your sever and run $ sudo crontab -e. Add this line at the end of the file. Make sure you replace the path to your WordPress root.


# m h dom mon dow command
*/5 * * * * php /PATH/TO/WORDPRESS/ROOT/wp-cron.php > /dev/null 2>&1

This tells linux server to execute WP Cron every 5 minutes and throw all output messages away. 5-minute interval is suitable for most WordPress sites, modify it if needed.

Visiting wp-cron.php Periodically

If you have no access to crontab, you can visit wp-cron.php via internet. Again, add this line to wp-config.php.

define( 'DISABLE_WP_CRON',  true );

Using Uptime Robot

Sign up a Uptime Robot account and make a HTTP(s) monitor to https://YOUR-WEBSITE.com/wp-cron.php?doing_wp_cron. Check my tutorial about Uptime Robot here.

Uptime Robot WP Cron Trigger

Do you need to Do it?

WP Cron is a must. Scheduled posts, automatic backup, auto-share to social media and all WordPress functionalities that perform in the future need WP Cron. Make sure you configure WP Cron to work properly.

However, if you are on WP Engine, they have a true cron that runs every minute on the minute for you. You don’t have to do anything.

This article is first posted on Ensure WP Cron Runs On Time



This post first appeared on Typist Tech, please read the originial post: here

Share the post

Ensure WP Cron Runs On Time

×

Subscribe to Typist Tech

Get updates delivered right to your inbox!

Thank you for your subscription

×