WordPress Cron Job setup tutorial for CPanel

WordPress Cron Job setup tutorial for CPanel
If you got that missed schedule issue on your website you might want to check this post first.

What is wp-cron.php?

To answer this question we should understand what CRON itself is. Cron is a time-based job scheduler in Unix-like systems (which means most hosting providers got it).These are typical cron jobs examples:

  • check for software updates everyday at 1am
  • make user’s databases backup twice a day
  • scan directories for malewares every Monday

Other words when you want to do something on regular basis cron is what you use. Whenever the task is scheduled to run on your server it will be executed automatically without any actions from user or system administrator.

WordPress comes with its own cron system which handle all recurrent tasks. Such tasks as checking core, themes and plugins updates are set by default.
These are typical WordPress Cron jobs examples:

  • publish pending posts twice a day
  • check whether user paid subsription is active and change his role accordingly everyday
  • delete revisions daily

You can schedule any event you want to happen at a certain interval. But WordPress Cron doesn’t work the same way unix cron does. It’s much more like simulator, though convenient, flexible and useful but still not real cron job.

What does wp-cron do?

As you remember unix cron is time-based system which will fire exactly on predefined time no matter whether someone is visiting website or not.

WordPress cron doesn’t work like that.

It triggers on page load only. When someone visits any website page it fires and checks all scheduled tasks time. And if scheduled time has already passed the task is executed. See the difference?

That means if you scheduled your post published on 7:32 am and your website traffic is not that big, your post most likely won’t be published exactly on that time. If the first visitor will attend your website at 8:40 am just then you’ll get it published. Someone has to visit your website to trigger wordpress cron.

This WordPress Cron peculiarity can cause some problems to new or not that popular websites. From other hand the websites with load of traffic may face another problem. As WordPress cron is triggered on every page view it may cause high CPU load and lead to serious performance issues.

No panic! We can manage it! What we gonna do is

Replace WordPress Cron with real Cron job in 3 steps

We’ll setup unix cron to handle our tasks instead of WordPress Cron in three steps:

  1. Disable wp-cron in WordPress wp-config.php:
    define('DISABLE_WP_CRON', true);
  2. Set real cron job instead
    1. Navigate to Cron Jobs in your CPanel
    2. Add new cron job command
      wget -O /dev/null http://yourwebsite.com/wp-cron.php?doing_wp_cron

      and set the time (Common settings already has the list). If you set 1 minute interval that means system cron will trigger your WordPress cron every single minute which garantee your scheduled tasks being executed exactly on scheduled time. But also it means significant server load. That’s why if it’s ok with your tasks to have some delay think of larger intervals, say 5 minutes or even once an hour.

  3. See your tasks done!

That’s was pretty simple, wasn’t it? Setting real cron job instead of wordpress built-in cron you can reduce server load and forget about missed schedule issue forever.

Share this stuff:

Leave a Reply

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