Change WordPress CRON jobs to real CRON jobs

WordPress is by nature capable of triggering so called cron jobs – but we all know those aren’t real cron jobs, because they are triggered only if you have visitors in your wordpress site. Real CRON jobs will run on scheduled interval based on system clock, despite of anything else – if so wanted.

Wordpress fake cron

Here is a quick recap on how to change those fake WordPress cron jobs to real cron jobs, either if you want to do so – or if your setup for some reason cannot execute wordpress scheduled cron jobs. This may be the case if your host is not allowing loopback connections and you don’t want to use the alternative cron approach with ugly looking post links.

Step 1 – disable the FAKE Wordpress cron jobs:

Insert or modify the wp-config.php to include following line

Select Code
1
2
3
4
/**
* Disable WordPress fake CRON jobs
*/
define('DISABLE_WP_CRON', true);

Step 2 – create a real CRON job in your crontab:
We want to create a real CRON that runs few times on an hour.

Create new PHP file in your wordpress root directory (I named it simply runWPcron.php), with the following contents:

Select Code
1
<!--?php include('wp-cron.php'); ?-->

Then modify your user crontab ‘crontab -e’ with following line :

Select Code
1
15,30 * * * * /usr/bin/php /var/www/html/runWPcron.php > /dev/null 2>&1

(Replace the path to your WordPress document root)

If you are uncertain where your binary PHP is – try asking it with ‘:-$ where is php’ command. And make sure you have PHP CLI installed – you can check that with command ‘:~$ php -v’ and look for cli in the response.

Above should work for most systems, but for example I had some challenges in having consistent CRON execution in my setup. Most probable reason was with changing directories as wp-cron.php actually triggers first wp-load.php process, which is located in the WordPress document root folder. So I decided to complement above CRON with another line:

Select Code
1
0 * * * * cd /var/www/html/ && /usr/bin/php /var/www/html/runWPcron.php >/dev/null 2>&1

After this addition, I’ve never missed any CRON job.

That’s it – now your WordPress CRON jobs will run every 0,15,30 minutes of every hour – despite if you have visitors or not. And there is also 30 minute gap with no running CRONs, that can be utilized for backups or similar.

Feedback

Juha Ketola

Juha Ketola

Author is a passionate IT enthusiast and early adopter. Packed with years of experience leading Enterprise level IT development teams within the biggest companies of Technology industry. Watches closely start-ups and new disruptive innovations in order to stay on the cutting edge. Embraces hands-on IT development and IoT.
Juha Ketola
Link to open Linkedin Profile

Leave a Reply

css.php