Neil Turner's Blog

Blogging about technology and randomness since 2002

A cron-ic issue

Screenshot of a cron crontab being edited

Over the past month you may have noticed that the ‘Links from Delicious’ posts appeared on Sundays, rather than Saturdays. This post is a rather long-winded explanation of why that came about.

cron

‘cron’ refers to the scheduling system of operating systems like Linux. Other Unix operating systems use it too, and although Mac OS X has ‘launchd’ instead it does a similar thing. It enables tasks to be run at set times or intervals, as a ‘cronjob’, rather than all the time. The list of tasks to run is usually installed in a ‘crontab’, and each user account has one. It can be accessed by typing ‘crontab -e’ in a Unix shell prompt, or using a package like cPanel.

cron in WordPress

WordPress needs to run scheduled tasks from time to time. The main use for this is scheduled posts – i.e. new blog posts that are scheduled to be posted at some time in the future. But some plugins like RSS Digest, which I use for the Links from Delicious posts, also use it. For this, there is ‘wp_cron’.

wp_cron is not the same as cron, however – it’s a kind of ‘faux-cron’ designed to replicate the functionality. When a visitor clicks a link onto a WordPress site, WordPress checks if the wp-cron.php script has been run recently, and, if not, it runs it. This will post any scheduled posts that are due, amongst other things.

The main advantage of this method is that it makes WordPress easy to install. There’s no need to edit crontabs. This compares favourably with Movable Type, which required a cronjob to be set up for various tasks and therefore added to the complexity of installing it.

But the disadvantage is that, on particularly busy sites, this wp-cron.php file gets hit very frequently. Indeed, it’s one of the most accessed files on my site.

Making WordPress use ‘real’ cron

So I decided to make WordPress use the ‘real’ cron. I found various guides, such as this one. Firstly, you edit your wp-config.php file to disable wp_cron, and then you add a line to your crontab to run wp_cron on a set schedule. This should reduce the number of hits on wp-cron.php and improve performance, whilst ensuring that scheduled tasks continue to run as normal.

The key word in that sentence is ‘should’. I tried various permutations of the crontab command, and none of them worked. Scheduled posts never got published, and RSS Digest couldn’t do its thing. I ended up manually over-riding RSS Digest to force the Links from Delicious posts to appear, which was usually a day late as I’ve been busy most Saturdays of late.

So, last week, I switched back to using wp_cron’s default settings, and now everything works.

It’s probable that I’ve done something wrong. For example, the crontab was created under my username, and not the username of the Apache web server. So I may try it again in future. But, for now, everything works, and the Links from Delicious posts will continue to appear on Saturdays, as intended.

One Comment

  1. If you don’t have adequate server Cron, try this WordPress plugin http://wordpress.org/plugins/easycron/