A Bristol Geek

No trees were harmed in the creation of this blog; however, a significant number of electrons were slightly inconvenienced.

WordPress External Scheduling (Cron)

Prague Astronomical Clock

Prague Astronomical Clock, now that's timing!

WordPress Cron

Firstly for those who do not know what CRON is, I shall explain.

Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word chronograph (a time-piece). Cron enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date.

Now class do we understand what I mean? No, well I don’t know what else I can say other than “it’s a scheduled task”, something that happens at a set time(s).  For example your alarm clock has the task “wake him the hell up” set for 8:00 (unless you are this man, who is always late for work on a Saturday!).

How does WordPress do it?

WordPress’ Cron system is not a true scheduling system, as there is no timer running in the background to ensure that actions occur correctly. Instead WordPress checks the list of jobs each time someone visits the site, if it finds a job that is due (or overdue) then it performs it.  For example, you may have scheduled a post to occur at 3:00pm, the first person to visit your website comes at 3:15pm the post will then be generated and shown to the next user.  So although you asked for the task to be completed at 3:00pm it didn’t actually happen until the first visit after that time.

This is not really a problem for scheduled posts as you have only lost one view, and when the user refreshed (or changes page) they will see the new post appear.  Where is can cause a problem is scheduled backups or your blog / database / etc. I use a plugin that can backup my entire WordPress directory and upload it to DropBox, having that run when a user visits the site causes them to have to wait for the pages to load.  Not really the best outcome, but there is a solution.

Manualy running the CRON

WordPress does allow you to run the Cron system manually, either by accessing a specific page or by running a command on your webserver.  Most web hosts provide you with some sort of basic task scheduling system (they may call it scheduled tasks / jobs rather than Cron), so you can have your server run the scheduled tasks in the background rather than from within WordPress.

The biggest advantage of this is that you control when the Cron tasks are run; rather than having to rely on a visitor to your site at the relevant time.  Secondly any tasks that are run at the server end are not preventing your users from viewing pages (however if you run a massive task on the server it may slow things down whilst it’s running).

How do I do it?

It is actually a very simple process to set up the Cron to run outside of WordPress, however I can not stress enough the importance of taking a backup before you make any changes to your site (this is the case regardless of what you are doing!).

Step 1:

You need to tell WordPress that you intend to run the Cron yourself, otherwise it will still try and run it every time someone visits your site. This is a simple one line edit to your WordPress configuration file, open up WP-Config.php from your sites root folder and add the following line after the end of the comments at the top of the file.

define(‘DISABLE_WP_CRON’,true);

This tells WordPress not to run the internal Cron System.

Step 2:

Set up a scheduled task on your webserver (or via your web hosting control panel) that calls the following:

php5 -q /<website path>/wp-cron.php

You will need to replace <website path> with the actual location of your wp-cron.php file.  It is likely to be something like “/home/sites/something/public_html/”, if you are uncertain ask your provider.

If your server doesn’t run PHP5, just remove the 5 from the command (so it just reads “php”).  The “-q” simply tells PHP to be quite and not tell you everything it is doing (else your log files will grow and grow!).

Step 3:

Test that it worked! Schedule a post / task (perhaps with backWPup) and see if it runs.

If you have any problems or questions feel free to put them in the comments on this page! Have fun!

3 thoughts on “WordPress External Scheduling (Cron)

  • angry analyst says:

    Having a look looks like my hosting doesn’t have the scheduled tasks

    • jamesakadamingo says:

      WWS does do Cron Jobs, but your blog-only package won’t come with it. There is a “Scheduled Tasks” add on that you can purchase through your account that will give you access to it.

      Or you could ask John nicely and see if he will give it to you for free (its £4.95 a year)

  • David Jeanneret says:

    Any ideas how this (and the backup plugin) interact with WordPress in network mode. I imagine the plugin would work fine – but is there a way to disable the wp-cron on a per site basis?
    Cheers
    David

Leave a Reply