Use external cron jobs on WordPress – The right method

This is part of our WordPress serie which is related to WordPress best-use techniques and administration.

Recently we switched over to the caching plugin WP Rocket. Serving cached files directly without calling PHP required us to disable WP Cron and to switch over to an external cron job respectively to the internal cron job which is running on every linux server, simply called cron.  Otherwise using the cache plugin may caused our scheduled jobs not to be executed. As you may already know WP Cron jobs are not real cron jobs and are executed only when there are visits on your site which calls the wp-cron.php file.

Doing so is usually not a big deal and only requires two steps:

  1. Disable wp cron with adding the line define(‘DISABLE_WP_CRON’, ‘true’);  to the wp-config.php
  2. Add a cron rule to the cron daemon

The rule is added to the cron table with the shell command crontab -e

Now we come to the point where we have to pay some attention. Several sites that aim to explain the cron rules show you a cron rule like below which is not working on every system. As a result the cron job is not executed:

*/5 * * * wget -q -O - > /dev/null 2>&1

The issue is that wget  isn’t in the default path used by cron so even when you are able to execute wget from the shell cron is not able to execute wget.
Give it the /full/path/to/wget and it will work:

*/5 * * * /usr/bin/wget -q -O - > /dev/null 2>&1

You can use the shell command find / -name wget  to find the path of it

You can also use curl instead wget without knowing the path of it:

*/5 * * * curl --silent "" > /dev/null 2>&1

The complete guide of the available cron rules is outside the scope of this article, but the rule above will trigger an event to fire every 5 minutes that will make a request to your WordPress website thus starting any scheduled tasks. You can read more here about how to change the time periods.

I hope this little helping article was useful for you.

Follow MashShare?