Monthly Archives: March 2014

What’s a Cron Job?

Cron Job

Cron Job…

What the heck is a Cron Job?!

According to Wikipedia,

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals. The name cron comes from the Greek word for time, χρόνος chronos.

To me, it’s something that I can use to make sure that my scheduled posts by WordPress aren’t missed! Because of the backlog from the last month or so, I didn’t want to put up 5 posts all at once. So I decided to go ahead and use the scheduling feature of WordPress. However, on my train ride home, I found that the post did not get put up by the scheduler and it said “Missed Schedule” in the status of the post.

Looking online yielded threads of WordPress users having similar issues. While Automattic provides fixes every now and then, it seems to be a little spotty in terms of coverage and I found that generally, every one gravitates to swapping the WP Cron to the real Linux Cron job.

WP Cron
From my readings, it seems that WP Cron is only triggered if there is a read to your site, in which case, when it loads a page for a viewer, it checks whether or not there are jobs queued up and executes them if there are. The problem with this approach is that sites that don’t get visited often (like mine), sometimes have problems triggering the delayed posting to be on time. If no one visits the site, then the post doesn’t get triggered and it is filed under “Missed Schedule”. In my case, since most of my traffic comes from WordPress’ publicize feature, which puts my post on the various social media sites (Twitter, Facebook, Google+, tumblr, App.net), it becomes a chicken and the egg problem. Without these links driving traffic, the WP Cron won’t fire and if it doesn’t fire, no one comes to the site. So alas, it was time for me to switch to using the real Linux Cron job.

Turning On the Cron Job
Now most of the guides on the internet for setting up a Cron job involves cPanel, a custom control panel from your hosting provider’s utility box that allow you to manage your site. But for those who have the ability to access the command line from their servers, you can edit your crontab directly.

sudo crontab -e

Add the line and replace your.site.com with your site address:

*/15 * * * * wget -O /dev/null http://your.site.com/wp-cron.php?doing_wp_cron

What the above setting does is set up your cron job to hit your wp-cron.php every 15 minutes to trigger the WordPress queue of things it needs to do. Once that’s set up, you won’t need WordPress to trigger it’s fake cron on page load, so you’ll need to disable it in wp-config.php by adding the following line:

DEFINE('DISABLE_WP_CRON', true);

Once that’s done, your WordPress Cron will be disabled and your site should be able to schedule posts using the real Linux Cron and have them published in a timely manner.

Hope that helps.
E

Useful Resources
How to set up your cron in Ubuntu:
https://help.ubuntu.com/community/CronHowto

How the WordPress Cron Works:
http://wp.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/
http://code.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress–wp-23119

Sites I looked at to figure out how to do this:
http://wpdailybits.com/blog/replace-wordpress-cron-with-real-cron-job/74
http://stevengliebe.com/2013/11/18/using-real-wordpress-cron-job-increased-reliability/