Run all due cron events for WordPress with WP-CLI

Running a real cronjob is much more reliable than WordPress’ built-in “maybe-will-trigger” solution. But if you’re running a multisite network, you have to add a crontab entry for every site you set up – which is tedious. Thanks to WP-CLI, we can use a small bash script instead, which will run all due events for all sites for us. Oh, and it works for single sites as well.


If you’re new to WordPress and cron jobs, I recommend you read Tom McFarlins’s Properly Setting Up WordPress Cron Jobs for more background info.


Here’s the script that does the trick:

How to use it

First of all, make sure you have WP-CLI installed. That’s out of scope for this post, but is well explained on the WP-CLI website.

Download the script, save it in e.g. your home folder as run-wp-cron.sh, edit the /path/to/wp string to point to your own WordPress directory, and make it an executable: $ chmod 0700 run-wp-cron.sh

Give it a test run: $ ./run-wp-cron.sh
It shouldn’t output anything.

Setup a cronjob

As a regular user on your system (i.e. not root), edit your crontab:
$ crontab -e

Add this line at the end of the file (replace the path to wherever you saved the script):
* * * * * /home/youruserdir/run-wp-cron.sh

This will make sure your system runs the script every minute. If there are no tasks to run, it will not add extra load to your system. If you do have tasks to run, well, then you want to run them.

Disable WP’s own cron spawner

In case you didn’t read the background post on WordPress cron jobs, it is overdue to disable WordPress’ own cron spawner by adding the following to your wp-config.php:
define( 'DISABLE_WP_CRON', true );

OK, now you’re done.

5 thoughts on “Run all due cron events for WordPress with WP-CLI”

  1. Hi @bjorn,
    Great Script, and as i know every time (every minute) the cron execute the bash, you will receive an email UNLESS you declare a log file, so it is better to append the output to some a log file :
    `* * * * * /home/youruserdir/run-wp-cron.sh >> /home/youryserdir/logs/wp-cron.log`

    1. Bjørn Johansen

      It really shouldn’t output anything, thus no email should be sent. But that is of course a nice way of creating a log file :-)
      If you want absolutely no output/log/email/whatsoever, you can redirect all stdout and stderr output to /dev/null as usual:
      * * * * * /path/run-wp-cron.sh > /dev/null 2>&1

  2. Great post.
    If you have no experience with setting up cron jobs on servers or don’t have access to do it, you can use a third party solution like Easycron.com who will call your wp_cron.php at a specific time.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.