CRON and WP Rocket
In this Article
What is Cron?
Cron is a standard utility in Unix and Linux systems, it is used to schedule commands for automatic execution at specific intervals. For instance, you might have a script that produces web stats and you want it to run once a day at a specific hour. Or imagine you need to run a PHP script on weekdays, every 2 hours.
You can't do these tasks manually and here is where Cron becomes useful. These commands involving Cron are referred to as " cron jobs."
WP-Cron vs "real" Cron jobs
WordPress has a feature called WP-Cron. WP Cron is a built-in function for automation which allows to schedule automated tasks, like publishing posts, maintenance tasks, update checks and by many plugins like WP Rocket which need to automate features.
WP-Cron is not actually a real cron job, you can think it is more like a “fake cron job”. These tasks are triggered when someone visits your site: during PHP page loads, WP Cron checks the database to see if there are any scheduled tasks to execute.
The way WP Cron works has some drawbacks:
- When you are using a caching plugin like WP Rocket, no PHP is executed in the frontend, so if there is no activity on WP Admin for some time, WP Cron processes would stop being triggered.
- It is also a potential issue on high traffic sites, since every page request might spawn a process that uses server resources
- This file can be used as the target of a DOS attack
Tip: To check if WP-Cron can run on your WordPress site: WP-Cron Status Checker
After activation, visit your Dashboard and look for the WP-Cron Status Checker widget.
Setting up a real Cron Job
Due to WP Cron drawbacks, is always recommended to set up a real Cron job on your server instead of relying on WP Cron.
- If you want to create a real Cron job, first you need to disable WP Cron, so is not executed every time someone loads one of your pages. To disable it, open the wp-config.php file in your main WordPress folder and add the following line before the "/* That's all, stop editing! Happy blogging. */" line:
- Add a Cron job entry through your web hosting control panel. You can set the job to run every 5 or 10 minutes.
Please remember to replace
yourdomain.comwith your actual domain:
wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Each server has different ways of adding a Cron job, so if you are not sure how to do it you can ask your hosting provider to help you.
You can check this short video if you are using cPanel: https://recordit.co/A4Jj1Kg7x9
Common issues related to Cron
If for any reason, scheduled processes are not properly working, you may notice issues with some of the following WP Rocket features:
- Cache Preloading
- Scheduled Cache Clearing
- Database optimization
- Critical Path CSS generation
- Google, Facebook and CloudFlare add-ons
Did you know?
You can extend WP Rocket functionality using Cron to launch custom functions.
For example: How to clear cache via cron job