WordPress – How to limit wp-cron cron job run times

By default WordPress will run an artificial ‘cron job’ once every minute.

These cron jobs are used to maintain WordPress in the background by doing tasks like publishing scheduled posts and triggering the wp_schedule_event action hook – which third-party plugins you have installed may be using.

There are two issues with this cron job –

  1. In some circumstances the cron jobs can be ‘stacked’ – meaning they can run more than one at a time, leading to excessive resource use and potentially your host shutting your website down.
  2. Checking and running the cron jobs every 60 seconds decreases the page load speed — especially if you don’t have any caching.

There are various solutions to this, some people choose to completely disable the cron job by using DISABLE_WP_CRON in the wp-config.php, for example

define('DISABLE_WP_CRON', true)

This of course would completely break scheduled posts and any other features that rely on the cron job.

Often this is taken a step further by setting up a real cron job using their host management tools (normally cPanel).

WP_CRON_LOCK_TIMEOUT

More recently another solution has been made available. WordPress 3.3 introduced WP_CRON_LOCK_TIMEOUT which allows you to define the period of time the cron job will run – this allows you to change it from the default 60 seconds to any other amount of time – such as 60 minutes (3600 seconds).

To configure this option you need to access the WordPress configuration file, usually you would use an FTP connection or access to the WordPress files using your hosts cPanel.

Open wp-config.php

At the very bottom add the following line, note that the number is seconds – 3600 is 60 minutes.

define('WP_CRON_LOCK_TIMEOUT', 3600);

Tagged in

2 comments on “WordPress – How to limit wp-cron cron job run times

  1. If you run the cron from the CLI, you don’t have to worry about Cloudflare, since CF will never see it as it rund directly from the server.
    More efficient, too!

  2. This is a GREAT solution for those struggling with trying to get Cron working when they are using Cloudflare.
    So…
    Instead of running Cron at the server level, re-enable WP cron (which breaks when using Cloudflare)

    Edit wp-config.php:
    define(‘DISABLE_WP_CRON’, false);

    Then use your solution in this post about the timeout, 10 minutes perhaps:

    define(‘WP_CRON_LOCK_TIMEOUT’, 600);

    Finally create page rule at Cloudflare’s end, so it’s not hitting the cached object instead:

    *domainname.com/wp-cron*
    Security Level: High
    Cache Level: Bypass
    Disable Apps
    Disable Performance

Leave a Reply to jason Cancel reply

Your email address will not be published. Required fields are marked *