Until now we used the cron system integrated with WordPress but as it can be read on many blogs posts, it doesn’t work well everytime. That’s why we have added a server cron job alternative to our products.
Why WordPress native cron job doesn’t work well?
First of all, WordPress native cron job only works when a page is loaded. So, if you have no visitor (ie. when starting a website), the cron will never be executed.
There may be several reasons why it doesn’t work:
- Conflict of plugins
- DNS-related issues
- WordPress bugs
- Use of caching plugins
- Large server load
In addition, our plugins perform rather heavy tasks:
- searching new videos
- excluding duplicates videos
- importing videos
- downloading thumbs
- updating feeds data
The more videos you already get, the longer it takes to find some new ones because every duplicated video has to be skipped. So the script has to look further to find new videos, and may crash with the ugly Fatal error: Maximum execution time of 30 seconds exceeded.
All these parameters explain why it has never been easy to offer a simple automatic import feature that works in 100% of cases. Just by using native WordPress cron.
The solution, the Server Cron Job
Step 1 – Copy this command
wget -qO- YOUR_SITE_URL/wp-cron.php &> /dev/null
Don’t forget to change YOUR_SITE_URL by your real site url (ie. https://www.mysite.com)
Step 2 – Create a new cron job via your Cpanel
Go to your Cpanel and create a new cron job.
Set the task as “Execute a command” and paste the command you copied in step 1
Step 3 – Add a frequency
For your information, the reasonable time interval is 5-15 minutes.
Set the frequency as a UNIX crontab. If you don’t even know what this means, just copy and paste this line:
*/5 * * * *
5 means that the cron will run every 5 minutes.
Step 4 – Disable WordPress native cron
By adding the server cron job, you have told your server to listen to WordPress cron every 5 minutes. But WordPress doen’t know that, and it will continue to run WP-Cron on each page load. You don’t need this any more.
Edit your wp-config.php file and add this line:
Here it is! You have just configured your Server Cron Job for WordPress.
How the Server Cron Job works?
When you have created your Server Cron, you may have noticed there is a “run now” button to run the cron manually. If you press it, you won’t see any new video on your site. Why?
The server cron doesn’t run the auto-update feature to search and import new videos directly. It “only” replaces the default WordPress native cron by checking for WordPress scheduled tasks.
Basically, WordPress doen’t look for scheduled tasks on each page load anymore. Now, the server cron does it every 5 minutes. If a scheduled task has to be run (ie. the auto-import scheduled task that can be run every 1/6/12/24 hours), it will be run.
In the end, we simply added a layer for the server to handle scheduled tasks instead of WordPress.