Running cron more than once an hour in Drupal 10

Cron is an essential component of Drupal that is used to automate repetitive tasks, such as checking for updates, sending emails, and cleaning up old data. By default, Drupal runs cron once an hour, but in some cases, you may need to run it more frequently. If you're looking for a way to run cron more often than once an hour, this article will help you do so.

Using a Module

One of the easiest ways to run cron more often than once an hour is to use a module. Two popular options for this are Elyisa cron and Ultimate Cron. These modules allow you to use a standard cron rule to define the frequency of your cron runs and can provide granularity down to seconds if needed.

The only drawback of using a module is that the more often you run cron, the more load there will be on your server. Testing and benchmarking are the best ways to determine the ideal cron frequency for your site.

Setting the Interval in settings.php

Another option for running cron more often than once an hour is to set the interval in your site's settings.php file. In Drupal 7, you can set the interval using the following code:

$conf['cron_safe_threshold'] = 300;

In Drupal 8 and later, you can set the interval using the following code:

$config['automated_cron.settings']['interval'] = 300;

Note that this approach uses the "automated cron" or "poor man's cron" feature of Drupal, which means that the cron job will be run on the next visit to any page that hits Drupal after the specified interval has passed since the last run.

This means that if your site has low traffic or uses edge caching, such as Varnish, the cron job may not run as often as you'd like.

Additionally, the value set in settings.php will not be reflected in the drop-down menu in the Drupal interface, which may be misleading to other developers or administrators who are working on the site.

Running a cron job from the server

If you have access to your server, you can run a cron job directly from the command line. This is a good option if you're using a hosting service that doesn't provide a user interface for managing cron jobs, such as Cpanel. To run a cron job from the server, you'll need to use a cron job manager to specify the frequency and command to run.

The exact steps for doing this will vary depending on your hosting service and operating system, but generally, you'll need to create a shell script that runs the cron.php file and set the frequency for the cron job using the cron job manager.

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!