Theme Graphic
Theme Graphic

Prog.news

see new entries of new operating systems , computer languages(like pascal), cheat engine ect.

Subscribe

Author

Archive

Tags

Posted on Wednesday, February 23, 2011 at 9:51 AM

Run Wordpress tasks from real cron job


Hi,

I recently worked with a Wordpress MU installation. As an ease of deployment and probably because it is largely used on shared hosting which may not offer access to cron jobs, Wordpress default way of processing its scheduled tasks is through front-end and admin page requests. As a result, everytime a page is requested, the system looks to see if there are jobs to run. When so a call is performed to execute the tasks. The asynchronous nature of the call makes it somewhat transparent from the webuser perspective.

Although it works well, I do not feel confident to use this mechanism on a site with a lot of traffic. After some google searches to find out how it works, I’ve read many stories about hosting companies denying access to wp-cron.php because of bad impact on their server.

I had another issue while using WP-SuperCache. Since the super cache is super because it prevents loading the whole PHP engine for guest requests. Unfortunately, no php code execution means no cron execution for super cached pages… Since the cron is responsible of cleaning the super cached files, they are served indefinetly or until a user logs in (logged users do not get super cached files).

So for all the good reason I had, I wanted to run Wordpress cron jobs from a real cron schedule. I had to pull off my sleeves, open WP code and see by myself because all I finally found was this article (well I must admit that it was very helpful): http://wordpress.org/support/topic/273381

To explain, in short, how Wordpress crons are working, here is a shortened call stack:

Web User Request => WP API initialisation ==> wp_cron() // if authorized to run, the cron array is loaded (from a get_option call) ===> spawn_cron() // this is called if a job pending execution is found in the cron array. ====> wp_remote_post(’wp-cron.php?doing_wp_cron’) // the asynchronous call wp_cron() is the function that initiate the process of executing the jobs if one needs to be executed. And wp-cron.php is the one that will, at the end, execute those jobs.



In order to run the jobs from a real cron, it is simple as scheduling a wget call to wp-cron.php and disabling PHP calls to wp_cron() function. Damn, if I knew that from beginning… When you open the wp_cron() function declaration, you will notice that first line checks for a constant definition: DISABLE_WP_CRON. So you define(’DISABLE_WP_CRON’, true) in your wp-config.php and that’s it.

Well, if you are using Wordpress MU, that’s not totally it. Remember the comments in the stack call above? The cron array is loaded from a function call to get_option. In Wordpress MU, get_option reads data from wp_xx_options which is per blog database table. So it has a set of scheduled tasks per blog. So you need to call wp-cron.php in the context of each blog. So it’s starting to bug me… I don’t want to do maintenance tasks everytime a blog is created in Wordpress MU! To prevent this, I created a small php script that I called wp-cron-multi-blog.php that is calling wp-cron.php for each active blogs defined in Wordpress MU.

Here is a wrap-up of the information without the bells and whistles. To run WP tasks from a real scheduled cron you should do this:

in wp-config.php, add the following line :

define('DISABLE_WP_CRON', true);

for a standard Wordpress installation, configure a crontab that:

wget http://www.server.com/wp-cron.php?doing_wp_cron for a Wordpress MU installation, configure a crontab per blog like:

wget http://www.server.com/wp-cron.php?doing_wp_cron wget http://www.server.com/whatever/wp-cron.php?doing_wp_cron wget http://www.server.com/what-a-blogger/wp-cron.php?doing_wp_cron ... or, a single crontab calling my wp-cron-multi-blog.php (placed in root installation of WP):

wget http://www.server.com/wp-cron-multi-blog.php A final note about wp-cron-multi-blog.php. If your Wordpress MU installation has a lot of blogs and each of them tends to have many time consuming tasks (ex: cache cleaning), it may be a good idea to edit this script in order to limit the number of blogs to run for at one time. (sorry about the links)
Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks   Share: Share By Email By Email

0 comments on "Run Wordpress tasks from real cron job"
No comments posted yet.

Leave A Comment
Subject:


Comment:
   Bold Italic Underline          Code Link Image Horizontal Rule


Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:


To help prevent comment SPAM, please enter the magic code '31' in the box:




Posting Rules
Please follow these rules when posting comments on blog posts.
  • Do not post anything that is racist, hate speech or of a sexual or adult nature.
  • Do not post or link to anything that infringes copyrighted laws.
  • Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
  • Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.
 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.