How to Replace WP-Cron with Linux Cron
Preface
From what I could gather, there are at least three ways to stop conventional wp-cron behavior, i.e. queue the jobs until the visitor arrives. We can technically make it run every 5-15 minutes or so, by the same way we had created a cron job to activate cron in the previous posts. The question is how and why.
First, you could try what I had done in my second post: using wget
or curl
. It didn’t occur to me as I was writing the how-to, but after digging through for more resources, it appears neither of the methods are particularly preferred due to network overhead. One blog even suggested that the hosting service may inadvertently block the cron as it may look like DDoS attack.
Second, if you have WP-CLI installed, you could create and manage cron jobs from there. Personally I couldn’t find the reason why I would prefer this method over what I will be writing in the instructions. Personally I’m not a fan of installing an intermediary tool to install another tool; and I happen to not have WP-CLI running.
Instructions
Overall idea is still the same as the second post. The difference is we are stopping wp-cron from activating per visitor, and rather have it run every 5-15 minutes. For cron schedule expression, —if your cPanel doesn’t provide one— you can use any tools freely available online. For example: */10 * * * *
means it will run every 10 minutes. Do be mindful, some hosting services will have various restrictions with cron, (e.g. only 5 simultaneous cron jobs) so you do need to check before moving forward.
- In the text editor, add the following line in the
wp-config.php
file, which will be inside the site’spublic_html
folder:
define('DISABLE_WP_CRON', true);
- From cPanel, add the following the command with cron schedule expression:
[schedule expression] [path to php] [path to wp-cron.php] > /dev/null 2>&1
On mine, the actual command looks like this:
*/5 * * * * /usr/bin/php /home/[username]/public_html/wp-cron.php > /dev/null 2>&1
The path for php could be different depending on the hosting environment, so do be careful. My hosting provider had used both /usr/bin/php
and /usr/local/bin/php
, so I had to test it out. The latter part of the command, after the wp-cron.php
is to discard the results. Again, depending on the service provider, some choose to email the results back to the designated recipient. Once you have set it up, you might want to discard any future results.
I have also updated the previous posts to consider disabling wp-cron entirely, as it seemed to be the standard practice. Again, I’ll update the posts if I find more about it.