How to Replace WP-Cron with Linux Cron

In my previous posts, I tried fixing WordPress’ weird behaviors with scheduled post not publishing correctly when the site uses a caching plugin. One such attempt was to add hooks to activate cache purging, and another was to forcefully activate wp-cron.php. I had hoped this would solve the problem, but I couldn’t get the stable results with neither of options —to the point where one of the recent posts weren’t published due to “missed scheduled post” error. The error seemed to be on parallel with the documentations I’ve read on wp-cron so far, so I decided to move on to something that I can get more resources on: 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.

  1. In the text editor, add the following line in the wp-config.php file, which will be inside the site’s public_html folder:

define('DISABLE_WP_CRON', true);

  1. 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.

Leave a comment