Cron for WordPress

Spread the love

Learn how to create and run jobs using Pantheon Cron or WordPress’s WP-Cron feature on your Pantheon site.

This section provides information on how to create and run Cron jobs.

Cron is a standard feature in Unix and Linux systems to schedule commands for automatic execution at configured intervals. These scheduled commands or tasks are known as cron jobs. Cron scheduling allows tasks to be executed at specified intervals ranging from once a minute to once a year.

Cron is generally used to:

  • Run scheduled backups
  • Monitor disk space
  • Delete files that are no longer required
  • Run system maintenance tasks

Pantheon Cron Overview

Pantheon Cron runs WordPress cron jobs as an hourly task or on demand through Terminus. This is in contrast to WP-Cron which executes jobs when a site is loaded by a visitor. Running cron on the platform provides a more reliable schedule and ensures visitors are not interrupted by a script triggered by WordPress cron.

Pantheon uses the following WP-CLI command to trigger cron on the platform:

wp cron event run --due-now

WP-Cron Overview

WP-Cron is a WordPress feature that executes jobs when the page is loaded. Jobs are executed before site content is served to the site visitor. Plugins and themes can add jobs to be executed at regular intervals. For example, if you have a plugin that scans Twitter for your tweets and then incorporates them into comments, it’s most likely done with a WP-Cron job.

The WP-Cron feature is designed solely to handle WordPress routine jobs as part of a page load, allowing you to:

  • Check for new versions of the WordPress core
  • Check for themes and plugins updates
  • Clean up spam

Understand the Differences Between Pantheon Cron and WP-Cron

There are some major differences between Pantheon Cron and WP-Cron.

When Jobs are Executed

Pantheon Cron runs WP-Cron as an hourly task or on demand through Terminus. In contrast, WP-Cron script values are checked and executed (if overdue) only when someone visits the site.

Info:

Note

Pantheon Cron will not execute jobs on inactive environments, including sleeping development environments.

WordPress Multisite

Pantheon Cron does not support WordPress Multisite installations due to the unpredictable customizations to domains or subdirectories and their mapping to subsites. Use WP-Cron if you have WordPress Multisite installations.

Security

WP-Cron is a PHP file that is usually located in the root directory of your WordPress installation. The wp-cron.php file is subject to DDoS attacks, but generally, wp-cron.php is secure.

There is only one parameter you can pass that will affect the script, doing_wp_cron. The $_GET value is not filtered, and is only used as a flag (not as an input for a process or variable). All inputs are ignored outside this. Currently, wp-cron.php does not have any known vulnerabilities or exploits, although it should be noted that no script on a server attached to the Internet is 100% secure.

Manage WP-Cron Internally

WP-Cron comes pre-configured and ready to run, so you don’t need to do anything to enable it on your WordPress sites.

Several jobs are automatically configured during the installation of WordPress. Use a few simple commands from the command line, or one of several plugins, to find the exact jobs that WP-Cron runs.

WP-Cron From the CLI

Use Terminus to see job details in WP-Cron. Terminus, through WP-CLI, provides details, such as:

  • What is scheduled to run
  • What will run next
  • What event hooks are configured

You can also use Terminus and WP-CLI to:

  • Schedule your own jobs
  • Execute existing jobs
  • Manage WP-Cron related features

Use the command below to test WP-Cron and ensure everything is working correctly. Replace <SITE_NAME> with your site’s name and replace <ENV_NAME> with the desired environment (“dev”, “test”, “live”, or the Multidev branch name.

terminus wp <SITE_NAME>.<ENV-NAME> -- cron test

You will receive the status of WP-Cron’s functionality. You will receive a response similar to the one below if this is a WordPress site on the Pantheon upstream now running on Pantheon Cron:

Error: The DISABLE_WP_CRON constant is set to true. WP-Cron spawning is disabled.

This is not an error, but indicates that WordPress’s internal cron scheduler has been deactivated and that the site will now rely on Pantheon Cron.

The result will look similar to the example below if this is a WordPress Multisite or has WordPress cron overridden:

Success: WP-Cron spawning is working as expected.

You can run any cron-related command with WP-CLI. Use Terminus when using WP-CLI to manage your Pantheon hosted WordPress site. The command format is:

terminus wp <SITE_NAME>.<ENV_NAME> -- cron <your wp-cron command and switches here>

All terminus wp commands require a site name and environment to operate. WordPress Multisites require an additional --url=<url> parameter to specify which subsite to run the command on.

Info:

Note

Terminus cannot execute cron jobs if you enable the Security setting on your Dashboard. You may receive status report errors on the Dashboard as a result.

Enable WP-Cron

Pantheon’s WordPress upstream disables WP-Cron by default in favor of Pantheon Cron. However, you can enable WP-Cron if you prefer to use it instead of Pantheon Cron.

  1. Locate the require_once expression that pulls in wp-config-pantheon.php in your wp-config.php file.
  2. Add the following code above the require_once expression to enable WP-Cron’s internal processing:
    wp-config.php
    define('DISABLE_WP_CRON', false);

Plugins

There are several plugins you can use if you want to keep an eye on WP-Cron but don’t like the command line. WP Crontrol, for example, shows all events scheduled for your site. You can create, edit, run, or delete jobs immediately from within your WordPress admin dashboard. You can also hook new actions into schedules or move existing actions to new schedules from within the Tools section.

WPX Cron Manager Lite works similarly with a slightly different UI. This plugin requires you to do a one-time installation of the WPX framework, which you can do straight from the plugin manager page.

Manage WP-Cron Externally

You can use external crons if you want more control over your site’s cron jobs, or if you don’t want WP-Cron or Pantheon Cron to handle jobs internally. This will solve the problems with high traffic and low traffic sites discussed in the Troubleshooting section.

Disable WP-Cron

Pantheon’s WordPress upstream disables WP-Cron by default.

Add the following code to your wp-config.php file to ensure WP-Cron is disabled if you are on a Custom Upstream that does not have this setting, or the site is a WordPress Multisite. This line must be above the require_once expression that pulls in wp-config-pantheon.php:

wp-config.php
define('DISABLE_WP_CRON', true);

Free Services

You must have a service that calls a URL at regular intervals after disabling WP-Cron, if you want more granular control over when Cron runs. The easiest way to do this is to set up an account with a free cron service:

Any of the above services will work. You can disable WP-Cron, but this also turns off automatic checks and calls to the wp-cron.php script. You must call that URL yourself using one of the services above. Most services only require the following setup:

  1. Set up an account.
  2. Set up a job that calls https://exampledomain.tld/wp-cron.php?doing_wp_cron.

You might have the ability to set up multiple jobs at different times depending on the service you use. Creating a single job that calls your site’s wp-cron.php script every 15 minutes is all you should need. You must adjust your job if you create new schedules that need to run more frequently than every 15 minutes.

Info:

Note

Do not add a value to the doing_wp_cron query variable. This variable must be empty for Cron to work correctly.

Using Your Own Server

You can use the Cron service to make a call to the wp-cron.php script if you administer your own server. You must learn how to correctly set up a Cron job and use wgetcurl, or a similar command to fetch a web page. Using your own server is not safer than using a web-based Cron service.

Troubleshooting

Problems With Low Traffic Sites

WP-Cron will skip jobs in WordPress sites with low traffic on WordPress Multisite installations that do not have Pantheon Cron. This doesn’t mean your page will be slow from previous jobs when someone eventually visits your site. Regardless of how many jobs WP-Cron has to execute, all jobs are run in the background so that your site’s performance is not adversely affected.

Low traffic WordPress sites on Pantheon are put to sleep after either one or twelve hours pass without site visitors (refer to idle containers for more information). Pantheon Cron jobs do not run in sleeping environments.

Problems With High Traffic Sites

You might run into problems with WP-Cron if your WordPress-powered site is high traffic. The main issues that come up are race conditions and long running processes.

  • Race condition: When more than one user visits your site and triggers WP-Cron to execute the same job. WP-Cron works hard to eliminate race conditions, but they can still happen, especially on high traffic sites.
  • Long running process: Any task that takes longer than the standard 60 seconds to run. Developers can adjust how long a PHP task is allowed to run with the set_time_limit() function. You can end up with more than one copy of wp-cron.php executing if this is set to be longer than the window between jobs.

Both of these issues are addressed within WP-Cron’s internal locking doc.

Pantheon Cron limits the occurrences of the issues listed above.

What is the maximum execution time of cron?

The maximum execution time of cron is 180 seconds (3 minutes).

FAQs

Frequently Asked Questions

What is a Premium Domain Name?   A premium domain name is the digital equivalent of prime real estate. It’s a short, catchy, and highly desirable web address that can significantly boost your brand's impact. These exclusive domains are already owned but available for purchase, offering you a shortcut to a powerful online presence. Why Choose a Premium Domain? Instant Brand Boost: Premium domains are like instant credibility boosters. They command attention, inspire trust, and make your business look established from day one. Memorable and Magnetic: Short, sweet, and unforgettable - these domains stick in people's minds. This means more visitors, better recall, and ultimately, more business. Outshine the Competition: In a crowded digital world, a premium domain is your secret weapon. Stand out, get noticed, and leave a lasting impression. Smart Investment: Premium domains often appreciate in value, just like a well-chosen piece of property. Own a piece of the digital world that could pay dividends. What Sets Premium Domains Apart?   Unlike ordinary domain names, premium domains are carefully crafted to be exceptional. They are shorter, more memorable, and often include valuable keywords. Plus, they often come with a built-in advantage: established online presence and search engine visibility. How Much Does a Premium Domain Cost?   The price tag for a premium domain depends on its desirability. While they cost more than standard domains, the investment can be game-changing. Think of it as an upfront cost for a long-term return. BrandBucket offers transparent pricing, so you know exactly what you're getting. Premium Domains: Worth the Investment?   Absolutely! A premium domain is more than just a website address; it's a strategic asset. By choosing the right premium domain, you're investing in your brand's future and setting yourself up for long-term success. What Are the Costs Associated with a Premium Domain?   While the initial purchase price of a premium domain is typically higher than a standard domain, the annual renewal fees are usually the same. Additionally, you may incur transfer fees if you decide to sell or move the domain to a different registrar. Can I Negotiate the Price of a Premium Domain? In some cases, it may be possible to negotiate the price of a premium domain. However, the success of negotiations depends on factors such as the domain's demand, the seller's willingness to negotiate, and the overall market conditions. At BrandBucket, we offer transparent, upfront pricing, but if you see a name that you like and wish to discuss price, please reach out to our sales team. How Do I Transfer a Premium Domain?   Transferring a premium domain involves a few steps, including unlocking the domain, obtaining an authorization code from the current registrar, and initiating the transfer with the new registrar. Many domain name marketplaces, including BrandBucket, offer assistance with the transfer process.