Setting Up a Real Crontab for WordPress


A Crontab is a job that runs programs at their specified intervals. In Linux, you can use crontab -e to edit the jobs, e.g. normally a line specifies one job, for example,

1
*/5 * * * * /usr/bin/uptime > /var/www/helloacm.com/htdocs/uptime.txt
*/5 * * * * /usr/bin/uptime > /var/www/helloacm.com/htdocs/uptime.txt

Adds a job that runs every 5 minute to redirect the output of command uptime to a text file.

For wordpress sites, there is indeed a wp-cron.php that locates at the root directory of your wordpress blog. Everytime the blog gets a visitor, it will trigger this file and check if there are jobs to do (e.g. should I do something?). If there is, then the PHP script will trigger a job in the background, which is claimed that it won’t slow down the page loading speed.

Does this slow down page loading speed? Probably not much or can be barely noticed any difference. However, as your traffic gets bigger, for each visitor, the server needs to check for cron jobs, which definitely increases the server processing time (server load).

So, why not put this under Linux crontab and disable that in wordpress. You can disable this in wordpress by adding the following to the wp-config.php

1
define('DISABLE_WP_CRON', true);
define('DISABLE_WP_CRON', true);

And next is to add this to crontab using crontab -e, the following runs the wp-cron.php every 5 minutes (change this frequency accordingly if you are not satisfied)

1
*/5 * * * * cd /var/www/codingforspeed.com;php /var/www/codingforspeed.com/wp-cron.php > /dev/null 2>&1
*/5 * * * * cd /var/www/codingforspeed.com;php /var/www/codingforspeed.com/wp-cron.php > /dev/null 2>&1

We have to cd into the wordpress folder otherwise the wp-cron.php may fail silently. However, this seems fixed in the latest WordPress.

1
2
3
4
if ( !defined('ABSPATH') ) {
        /** Set up WordPress environment */
        require_once( dirname( __FILE__ ) . '/wp-load.php' );
}
if ( !defined('ABSPATH') ) {
        /** Set up WordPress environment */
        require_once( dirname( __FILE__ ) . '/wp-load.php' );
}

You might also use curl or wget or do this:

1
curl https://helloacm.com/wp-cron.php
curl https://helloacm.com/wp-cron.php

Alternatively,

1
wget https://helloacm.com/wp-cron.php > /dev/null 2>&1
wget https://helloacm.com/wp-cron.php > /dev/null 2>&1

You might want to check the 301 redirections if the URL specified has redirections. You can use the online utility at https://helloacm.com/curl/.

https-helloacm-301-redirection Setting Up a Real Crontab for Wordpress apache server linux wordpress

301 redirections

After setting this up, your linux OS will do the crontab instead and the page processing time will be reduced, irrespective with the traffic amount.

Many hosting doesn’t provide adequate Cron function, and the users need to look for an external Cron service and one alternative would be using easycron.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
576 words
Last Post: How Much In Theory Can You Earn With Adsense Within Your Bandwidth Allowance?
Next Post: Bash Shell Coding Exercise - Checking If Given Number is Prime

The Permanent URL is: Setting Up a Real Crontab for WordPress

Leave a Reply