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 that allows to schedule automated tasks, like publishing posts, maintenance tasks, update checks, and by many plugins like Visual Portfolio Pro which need to update Social Networks data.
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 traffic on your site is low, Crons may not run when scheduled. Instead, tasks will wait for a visitor and run all delayed tasks at once
- The 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 DDoS attack
Tip: Quickly check if WP-Cron can run on your WordPress: 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.phpfile 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 1 or 2 minutes.
Please remember to replace
yourdomain.comwith your actual domain:
wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
If you’re not comfortable with setting up Cron jobs on your server or your hosting provider doesn’t provide access, you can also use a third-party solution like https://cron-job.org/.