Replacing WordPress wp-cron with a cron job

There is a build-in feature in WordPress for scheduling various events, which is called wp-cron. For some reason or other it seems to work very sporadically, and in my case for backup scheduling, most of the time it didn’t work at all, causing missed backups. So, I had to do something about it…

On servers running on a Linux machine there is another much more reliable option for scheduling tasks: Linux’s own cron. In the next tutorial I’m trying to explain the required steps to switch from wp-cron to a real cron. I’m assuming here that you have at least basic Linux/Unix skills.

1) First, edit wp-config.php and add the following line to disable WordPress built-in cron

1
define('DISABLE_WP_CRON', true);

2) Add a cron job using

1
crontab -e

In this example I want to do the backup to Dropbox (using BackWPup plugin) on Fridays at 18.00. For more information about the crontab syntax please see the various cron tutorials in the web.

1
* 18 * * 5 wget -O /dev/null http://yoursite.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

Replace ‘yoursite.com’ with your website URL.

3) Test it

1
wget -O /dev/null http://yoursite.com/wp-cron.php?doing_wp_cron

If this works without error, all is well, and your new scheduling should be working. But if you get an error message like ‘403 Forbidden’, something is seriously wrong. This error caused me a lot of headscratching, but I finally found out that the BulletProof Security plugin was blocking wget as a User Agent. Consequently I needed to do the following alteration to the .htaccess file

1
2
- RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC,OR]
+ RewriteCond %{HTTP_USER_AGENT} (libwww-perl|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC,OR]

In short, if you have any problems with the cron, please double- and triple-check your .htaccess settings!

This entry was posted in Linux and tagged , . Bookmark the permalink.