How to disable the wp-cron.php in WordPress and use Crone rules

0
203

In the previous post, I installed WordPress on Nginx.
Then I configured the site backup module and noted that WordPress uses a file called wp-cron.php as a virtual cron-job to automate things like checking for plugin or theme updates, sending email notifications and more. I also found Xclonner Team’s recommendation to disable wp-cron configuration in WordPress and setting up wp-cron.php to run manually.

In this post, you can find an example of a cron-job configuration in Ubuntu 16.04

Base information

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. Cron is most suitable for scheduling repetitive tasks, for example, my blog backup operations. By the way, if you use cPanel File Manager Code Editor you can choose this manual.

Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. Often, in modern distributions, in crontab can be found the run-parts utility, which can run the necessary scripts from the following folders:
 /etc/cron.minutely /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly

Disable default wp-cron.php behavior

Open your wp-config.php file

vim /var/www/blog_galkinc_ru/html/wp-config.php

Go to the bottom of the database settings in wp-config.php typically around line 39.
Add the code below:

define('DISABLE_WP_CRON', true);

Cron configuration

For checking the current configuration you can use

crontab -l

For configuration, you should use the command:

crontab -e

If you want to create cron for “www-data” user you should use “-u” key

crontab -u www-data -e

Note, that the rules update and save to /var/spool/cron/crontabs/<user_name>, and the new processes will be launched from the user from whom you added them.

Create a cron-job

By the recommendation, running the wp-cron.php script every 6 hours is perfectly fine. That would be just four executions in a day, compared to possibly hundreds, or even thousands if you had a lot of website traffic that day.

Add the rule for php7.2 wp-cron.php running (in quiet-mode. Suppress HTTP header output):

0 */6 * * * php7.2 -q /var/www/blog_galkinc_ru/html/wp-cron.php

That all. The PHP file runs every 6 hours.

Debugging

For debugging you can analyze the log file, it can be /var/log/cron or /var/log/syslog

grep CRON /var/log/syslog

If you want to verify if a crontab is running and not have to search for it in cron.log or syslog, create a crontab that redirects output to a log file of your choice:

0 */6 * * * php7.2 -q /var/www/blog_galkinc_ru/html/wp-cron.php >> /var/www/blog_galkinc_ru/logs/crontab.log 2>&1

More information about cron debugging you can find here.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

LEAVE A REPLY

Please enter your comment!
Please enter your name here