Setting Up Reliable WordPress Cron

We all know wp-cron can be unreliable if left on default WordPress setting, WordPress’s behaviour on maybe I will run maybe not for wp-cron in a case of sites with less traffic.

There are following solutions to solve this problem. All require to disable default WP cron.

This will work for both single and multisite WordPress installation.

  1. Linux Crontab
  2. For Scalable and time-sensitive cron jobs.
  3. Sites like Pingdom and Uptimebutler (Not covered)

1. Linux Crontab

https://gist.github.com/PatelUtkarsh/3c50655d3606943b5801652a6aaa5634

This script will work on both WordPress multisite and single site.
https://gist.github.com/PatelUtkarsh/3c50655d3606943b5801652a6aaa5634.js

WP_PATH="/path/to/wp"

Make the script executable:

chmod +x execute-wp-cron.sh

Test it by running ./execute-wp-cron.sh If it does not output anything go to next step else fall back.

Every user has their own crontab so I’ll recommend editing your Nginx / Apache user’s crontab ( www-data or http )

To edit run:

crontab -e -u {user-name}

And add the following line and update your script path

* * * * * /some/folder/execute-wp-cron.sh

This will tell Linux to check for wp-cron every minute and if there is any new cron yet to execute in a pipeline it will execute it.

You can update cron time according to your preference, This site https://crontab.guru may help to understand parameters.

2. Scalable and time-sensitive cron jobs.

If you want a highly scalable cron then I strongly recommend checking the following repo:

  1. https://github.com/humanmade/Cavalcade 
  2. https://github.com/Automattic/Cron-Control

☝️It’s recommended for sites with high traffic which is relying on cron and running time-critical cron.

Both require different setup you can find that on their github/product page and pick one which you feel works for you.

Disable default cron

Lastly, disable wp-cron by adding this line in wp-config.php

define( 'DISABLE_WP_CRON', true );
// Mind space WPCS.

You can test if cron is running by wp cron event list

And that’s it. ?


Comments

Leave a Reply

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