How to check if cron is running correctly
WP Rocket's Preload, Remove Unused CSS, automatic cache clearing, and other features, heavily depend on cron.
So, whether you're using WP Cron or a server side cron job, you should make sure the frequency of occurrence is reliable and high enough.
In general term, or for most websites, the goal is to have a cron call at least every 5 minutes.
In this article you will learn how to check the status of cron, and how to log cron's ocurrence, manually or using a plugin.
Checking WP-Cron with a plugin
You can install a third party plugin such as WP-Cron Status Checker. If WP-Cron is enabled, the WP-Cron Status Checker widget in your WordPress dashboard should show the following:
The widget will show if WP-Cron is able to run and the last time it succeeded:
WP-Cron is able to run as of: October 5, 2022 3:05 pm
Last time WP Cron succeeded: October 5, 2022 4:05 pm
Checking WP-Cron manually
You can check if WP-Cron was disabled by checking your wp-config.php file and looking for this line:
define('DISABLE_WP_CRON', true);
When the above line is added, it means WP-Cron was deactivated. In most cases, this also means that a server side cron job was set up instead.
Checking server side cron settings
Now, if WP-Cron was deactivated, and you have a server side cron job running. Please make sure the following conditions are met:
- The request needs to be done to the wp-cron.php file. The most popular command is the following:
wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
You can see this and more information about setting up cron jobs for single and multisite installations in this article.
Checking cron occurrence
If the WP Cron frequency is frequent enough and reliable, then short of other issues, the preloading, Used CSS generation or the other scheduled tasks should be able to run as expected.
You can log WP Cron occurrence by adding the following code:
// added to log the wp-cron calls error_log( "\n [" . date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . "] Cron: " . print_r(($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Triggered by the server', true), 3, "cron.log" ); // end of addition
Add it at the top of the wp-cron.php file, after the opening comment block, and before the ignore_user_abort( true );
line.
Ideally, the resulting cron.log file will be located at the same level as wp-cron.php, and will look something like this:
[2022-11-15 20:00:00] Cron: Triggered by the server [2022-11-15 20:01:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:02:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:05:00] Cron: Triggered by the server [2022-11-15 20:06:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:07:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:10:00] Cron: Wget/1.14 (linux-gnu) [2022-11-15 20:13:32] Cron: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) [2022-11-15 20:15:00] Cron: Triggered by the server [2022-11-15 20:16:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:17:00] Cron: WordPress/6.0.2; https://yourdomain.com/ [2022-11-15 20:20:00] Cron: Triggered by the server
In the above log file, you can see cron is being triggered every 5 minutes from the server, every few minutes from WordPress, and there's a browser generated cron visit too.
If your log file shows a similar result then this will mean cron is running as needed.
The server triggered cron could be logged as the command, Cron: Wget/1.14 (linux-gnu)
, or as Cron: Triggered by the server
depending on the server configuration.
A backend triggered cron can be logged as Cron: WordPress/6.0.2; https://yourdomain.com/
, or as Cron: Wxfo4jfv6u
, depending on the configuration.
If there are problems with cron periodicity, the preloading, Used CSS generation and other tasks will be slow or will get stuck.
Logging cron's ocurrence with a helper plugin
If you don't feel comfortable making these code changes you can install the following helper plugin.
📥 Download (.zip): WP Rocket | Log Cron Periodicity
Developers: You can find the code for this plugin on GitHub.
This helper will create a log file wpr-cron-periodicity.txt
in the WordPress installation directory (e.g. public_html) which will record the periodicity with which the WP Cron is executed.
To avoid creating a huge log file, remember to delete this helper plugin once you're done logging the frequency.