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:
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
define( 'DISABLE_WP_CRON', true );
OK, now you’re done.