Using cron to Trigger wp-cron.php

WordPress was designed to run in various hosting environments without additional configuration through targeting the lowest common denominator — shared Linux hosting running php4. Since most hosting companies do not provide support for cron and configuring cron can be imposing to the average user, WordPress includes its own version of “cron” for scheduling tasks, which relies on a visitor request to trigger the execution of wp-cron.php.  This approach in essence occasionally hijacks a request for content to execute scheduled tasks, which is less than ideal and does not offer much in the way of control to the sysadmin.

For the more experienced sysadmin/developer that uses more sophisticated hosting (VPS, dedicated server, or even shared hosting with cron support) there is another option: using cron to execute wp-cron.php. First, wp-cron needs to be disabled in the configuration section of wp-config.php.

define('DISABLE_WP_CRON', true);

Then, simply request wp-cron.php directly using cron. For more information on cron, the Wikipedia article is a good starting point.

To edit a user’s crontab, log into the server via ssh and issue the following:

$> crontab -e

If you don’t use ssh often, you may also need to set the $EDITOR environment variable to an editor that you are comfortable using (e.g. vim, emacs, nano, pico, etc…).

There are two different approaches that can be used to execute wp-cron.php.

1. Use wget or curl to issue a HTTP request for wp-cron.php

*/30 * * * * wget http://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

2. Write a small wrapper script to setup the environment and execute wp-cron.php directly.

*/30 * * * * /usr/bin/php /var/cron-scripts/run-wp-cron.php /dev/null 2>&1
<?php
chdir('/var/www'); // WordPress install DocumentRoot
include('wp-cron.php');
?>

In both examples, ‘/dev/null 2>&1′ is used to send both stdout and stderr to /dev/null which discards any output and errors. By default, cron will send an email to the email address defined for the user.

10 thoughts on “Using cron to Trigger wp-cron.php

  1. Gregory
    Very helpful post thanks – believe it or not the coloured coding above really helps. I haven’t quite figured out what all the wildcard “*”s in your code above are… but I will do that. I had server overload problems due to cron jobs being triggered by visitors at peak times. I’d like to set my cron jobs (like daily database backup) to run when my traffic levels are lower.

    1. Take a look at the Wikipedia article on Cron, which explains that each column represents a time cycle. The asterisk tells cron to execute the script on every cycle (minute, hour, day, week, month, year).

  2. Your post is really helpful as codex does not not say anywhere , how to configure wp-cron.php using shell.
    I did before using following command and was able to run file but no wordpress enviroment.
    */30 * * * * /usr/bin/php -f /var/www/user/wp-cron.php

    Now am going to try your comman using wget. But i dont knwo what these “/dev/null 2>&1″ means.
    If it is for log file , can i write “>> /cronlog.txt”
    Any help will be appreciated.
    Thanks in advanced.

    1. I updated the post to add a little more detail. “2>&1″ tells the shell to redirect stderr to stdout. You probably want to do the same if you are writing the output to a log file. Don’t forget to rotate the log.

    2. I believe, never late to reply:

      You might have encountered error like “PHP Warning: require_once(./wp-load.php): failed to open stream: No such file or directory….”

      Try like this: */30 * * * * cd /var/www/user && /usr/bin/php -q wp-cron.php

      -q means quiet execution.

      Because wp-cron.php has call to wp-load.php to load wordpress configuration

  3. Hello

    Great article, thank you. However I do not understand why wp-cron.php isn’t directly called via PHP, instead you have chosen to create a new file and use the PHP function `include` to include the actual wp-cron.php file – why is this?

    1. The wrapper script was used to change the current working directory to the root of the WordPress installation. Having a separate cron script also is useful if you want to output additional logging information such as the execution time.

  4. I was getting “suspicious process” alerts from CSF from my wp-cron.php file taking a very long time to process. This helped a lot by being able to runt he cron at a more uniform time. Thanks!

Leave a Reply