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.
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.
11 thoughts on “Using cron to Trigger wp-cron.php”
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.
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).
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.
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.
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
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?
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.
You saved me from my cron job misery. 🙂 Thank you.
Not too advanced in editing via SSH. Would setting up my cron jobs through cPanel > Cron Jobs > Manage Cron Jobs accomplish the same thing, I have set it up as;
Timing: */15 * * * *
Command: wget -q -O – http://kleanathletecanada.ca/wp-cron.php?doing_wp_cron >/dev/null 2>&1
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!
How can I change so that the regular cronjob looks in another folder than /www? My server log is spammed with failing attempts because wp-cron.php isn’t located in /www.