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 every time. That’s why we have added a server Cron job alternative to our products.
Table of content
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 doesn’t know that, and it will continue to run WP-Cron on each page load. You don’t need this any more.
To disable this native WordPress behavior, we need to add a line in the wp-config.php file.
To edit the wp-config.php file, follow those steps:
- Login to your WordPress hosting account’s cPanel or use an FTP client to connect to your site’s server.
- Navigate to the root folder of your WordPress site.
- Look for the “wp-config.php” file. It should be in the same directory as your WordPress files such as “wp-admin”, “wp-content”, and “wp-includes”.
- If you cannot see the file, make sure that you have enabled the display of hidden files in your FTP client or hosting account’s file manager.
- Once you find the wp-config.php file, you can edit it using a text editor.
- Add the following line anywhere in your wp-config.php file:
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.