Replace WP-Cron with real cronjob for high traffic sites

Replace WP-Cron with real cronjob for high traffic sites

Remove query strings from static resources on WordPressYou 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

Replace WP-Cron with real cronjob for high traffic sites

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!

Rating: 5.0/5. From 1 vote.
Please wait...
Anonymous

Jules

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

You may also like...

1 Response

  1. shahida shahida says:

    thats fine

    No votes yet.
    Please wait...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »