Identify Failing Cron Jobs

WordPress cron jobs are scheduled tasks that perform various background operations, such as sending email notifications, generating sitemaps, and updating plugin data. While these tasks are essential for maintaining a functional WordPress site, they can sometimes fail due to various factors, such as server resource limitations, plugin conflicts, faulty code, or incorrect cron configurations.

If cron is failing, it is crucial to be able to identify and troubleshoot to ensure your WordPress site operates smoothly. In this tutorial, we will show you how to debug and identify a failing WordPress cron job, and provide essential tips and tricks to resolve these issues effectively.

Suggested prerequisites

  • A WordPress installation
  • Familiarity with SSH is recommended

Checking for cron job failures

The first step in addressing cron job failures is to determine if they are failing in the first place. Several methods can be used to identify failing cron jobs:

  1. Cron emails: When using server-side cron, our servers will send you emails if there is any error output from the cron jobs at runtime. These emails will contain the command executed on the server in the title, along with the output from the cron jobs
  2. Reviewing logs: Many WordPress plugins and themes log errors related to cron job failures. Check your site’s ErrorLog and cronlog, which can be found in the ~/logs/ directory.
  3. Using monitoring plugins: Plugins like WP Crontrol provide detailed monitoring of cron job execution, including success or failure status, execution time, and any error messages.
  4. Checking cron event list: The WP-CLI command ‘wp cron event list’ can be used to display a list of all scheduled cron events and their next occurrence, helping you identify any missing or delayed jobs.
  5. Manually running cron jobs: To verify that cron jobs are functioning correctly, you can manually execute them from the command line using WP-CLI. Use the command wp cron event run --all to execute all scheduled cron tasks. If any jobs fail, you’ll receive an error message indicating the specific task and the reason for its failure. Here’s a general example of what a cron-related error might look like:

Error: The request failed. HTTP error: 404 Not Found

In this example, the error indicates that there was a request failure with an HTTP 404 status code. This could happen if the cron is unable to find the specified cron event or if there’s an issue with the URL or endpoint associated with the cron task.

The following example below shows the output of a successful wp cron event run --all command, free from any errors or output:

Image of a successful output by running wp cron event run --all with no errors

Investigating failed cron jobs

Once you’ve identified a failing cron job, it’s time to investigate the underlying cause of the failure. The specific steps involved will vary depending on the nature of the cron job and the error message given. However, some general troubleshooting techniques can be applied:

  1. Review the cron job: Check that the cron job is correct. By manually running the command. This lets you confirm its functionality and the expected behaviour, independent of cron scheduling. For example, if the cron command is wp cron event run --all --path=/kunder/examp_12345/users_12345/public, log into the site via SSH and execute it directly in the terminal from your root directory to verify it is working before checking cron logs or other tools.
  2. Check plugin conflicts: Disable any plugins that are related to the failing cron job and see if the issue persists. If disabling the plugin resolves the issue, the plugin is likely causing a conflict.
  3. Review your code: If the cron job is custom-coded, carefully review the code for any errors, inconsistencies, or missing dependencies. Ensure that the code is compatible with your WordPress version and PHP versions.
  4. Test environment: Create a test environment to isolate the cron job and replicate the issue. This can help with debugging and to identify if the problem lies with the server configuration, access permissions, your code, or external API calls.

Additional tips and tricks

  1. Enable WP_DEBUG mode: Set the WP_DEBUG constants to ‘true’ in your wp-config.php file to enable detailed error reporting. This will provide more information about any errors that occur during cron job execution.
  2. Check plugin updates: Regularly check for plugin updates and ensure that all plugins are up-to-date. Outdated plugins can introduce compatibility issues or contain bugs affecting cron job execution.

Conclusion

Debugging failing WordPress cron jobs can be a challenging task, but by following a systematic approach and utilising the tips provided, you can effectively identify and resolve these issues. Remember, maintaining a functional WordPress site requires regular monitoring, troubleshooting, and optimizations.

If you want to learn more about setting up correct cron jobs for your CMS, you can explore the following articles: