How to run WordPress cron with WP CLI via crontab on Cloudways

Sometimes you need to run WordPress cron a non-standard way, in order to guarantee successful execution on a regular schedule.

The issue with “standard” WordPress cron tasks (which are a pseudo cron in themselves) is these are processed upon certain triggers, which run under PHP as your admin is loaded. This can both slow life down for administrators, as well as making cron tasks a little inconsistent. Worse, if you have big cron tasks, sometimes these are hit by timeouts.

The solution is to run cron under WP CLI (the installation of which on your server is outside of the scope of this tutorial), which uses PHP CLI and therefore is not subject to the same timeouts as PHP might be in your production environment.

Before You Start – Disabling WordPress Cron

Before you add this as a real cron task, you should disable the standard WordPress cron, by adding the following to your WordPress wp-config.php file:

define('DISABLE_WP_CRON', 'true');

The Regular Cron Task

You can run these under real sever cron, using a task like:

*/2 * * * * cd /home/webroot/; wp cron event run --due-now > /dev/null 2>&1

This would run cron every 2 minutes under Ubuntu linux – tested and works.

The Cloudways Cron Task

However, what if you are not root, or if you’re using a managed service such as Cloudways (for which this tutorial is posted)? Well, you’re going to need to use a slightly different method.

First, we’ll use && to add our second command, secondly, we’ll need to specific where “wp” is located, in this case that is `/usr/local/bin/wp` – giving a command which looks like this:

*/1 * * * * cd /home/master/applications/[APPLICATION-ID]/public_html && /usr/local/bin/wp cron event run --due-now

Remember to replace [APPLICATION-ID] with your application ID, which can be found here in Cloudways panel – Click Applications, then see this to find it:

Where to Insert Cron Task in Cloudways

Insert this in Cloudways under the application > Cron Job Management > Advanced Cron – it should look like this:

Signup With Cloudways

We really like Cloudways for managed WordPress hosting, particularly for hosting WooCommerce stores, because their config and automatic options make your life easier when it comes to server administration. Manage your application, not your server.

If you’re signing up, please use our referral link, and enter SILICONDALES20 during registration for 20% off your first invoice.

Leave a comment