Categories: How-ToWordpress

Replace WP-Cron with real cronjob for high traffic sites

Replace WP-Cron with real cronjob for high traffic sites

You all already know the pseudo cron which comes with wordpress is a pain and get called to often, to be honest at every visit of your blog. So i will give you some guidance to solve this issue.

The most described solution i found on the internet was doing cronjob for wp-cron.php by fetching the url: http://your.url/wp-cron.php?doing_wp_cron via wget/curl or equal.

But for me that was not sufficient since you can ran into problems when your blog using some security plugins which prevents remote execution of wp-cron. Remember the time as wp-cron.php got compromised a lot in past.

Another solution some other blogs describe is to do it by changing to wordpress webroot location and executing the script directly.

Well thats fine but since i wanted to automate this task even more and apply to multiple wordpress installations on same server i solved this by writing a small wrapper script which gets executed by the cronjob and then doing all the path finding to the wp-cron.php location itself.

First of all here is an example how your multiple server directory structure should look like on your webserver:

/storage/website.one/htdocs
/storage/website.two/htdocs
/storage/website.three/htdocs

We also asume in this tutorial that our webserver is running with userid/group: www-data and php cli is located at: /usr/bin/php

So let’s do it!

First of all open a empty file like wp-cronjob.php with your preferred text editor and save it to /storage/wp-cronjob.php with the following content:

// replace this search pattern to match your path
$vhostpath = '/storage/*/htdocs/wp-cron.php';

foreach (glob("{$vhostpath}") as $wpcron) {
     if(file_exists($wpcron))
     {
         //file found, we change dir and execute it
         chdir(dirname($wpcron));
         include $wpcron;
     }
};

We need to give our cron wrapper the right privileges like we want to run our cronjob with same rights as the webserver userid/group:

chown www-data:www-data /storage/wp-cronjob.php

Edit and attach following code snippet before the line: “/* That’s all, stop editing! Happy blogging. */” in your wp-config.php file:

define('DISABLE_WP_CRON', 'true');


And finally we prepare the linux cronjob to run our wrapper every 10 minutes by opening empty file:
/etc/cron.d/wp-cronjob in our preferred text editor and attaching this line:

*/10 * * * * www-data /usr/bin/php /storage/wp-cronjob.php >/dev/null

A good description about the cronjob scheduling definitions you can read: Here

Now check if your cron task is running correctly every 10 minutes by watching the syslog:

tail -f /var/log/syslog

And there we go!

Jules

Jules is the owner and author of ISPIRE.ME. He's a Linux System Engineer, Tech fanatic and an Open Source fan.

View Comments

Recent Posts

HTTP/2 SSL Offloading with Hitch and Varnish

HTTP/2 SSL Offloading with Hitch and Varnish Since Chrome browsers showing you insecure warning on…

7 years ago

File changed as we read it GlusterFS issue

File changed as we read it GlusterFS issue Recently i had trouble running Backups of…

8 years ago

Running multiple instances of varnish using systemd

Running multiple instances of varnish using systemd If you have not yet found a complete…

8 years ago

HTTP/2 SSL Offloading with Haproxy and Nginx

HTTP/2 SSL Offloading with Haproxy and Nginx After HTTP/2 becoming more an more prominent regarding SSL…

8 years ago

Get Real IP with Haproxy Tomcat Jira Confluence using x-forwarded-for

Get Real IP with Haproxy Tomcat Jira Confluence using x-forwarded-for Everyone knows the Problem. Get…

8 years ago

Review TDS2 How to backtest using tick data with Metatrader 4

Review TDS2 How to backtest using tick data with Metatrader 4 in this Review TDS2…

8 years ago

This website uses cookies.