Documentation Level: 
Advanced
Documentation Status: 
No known problems

Periodic tasks your website does on its own

Setting up cron is an important step in the installation or deployment of a Backdrop website. Cron assists with many tasks necessary for regular maintenance of the site.

A properly configured cron can manage a variety of tasks:

  • The Search module indexes the website's content.
  • The System module performs routine maintenance tasks such as pruning of logs, and removing temporary files.
  • The Update module checks for updates to Backdrop core and contributed projects

What is cron?

Cron (short for Chronos, the personification of time in Greek mythology) is a daemon that executes commands at specified intervals. The commands the daemon executes are called "cron jobs". Cron is available on Unix, Linux and Mac servers. (Windows servers use a Scheduled Task to execute commands instead.) 

The actual "cron job" is a time-triggered action that is usually (and most efficiently) performed by your website's hosting server, but can also be configured by a remote service or even from your own desktop.

For Backdrop, we would like the  daemon to visit the core/cron.php file in your website at a URL like http://www.example.com/core/cron.php?cron_key=0MgWtfB33FYbbQ5UAC3L0LL3RC0PT3RNUBZILLA0Nf1Re. You can find the exact address of the cron.php file in the "Status report" page, at Administration > Reports > Status report (admin/reports/status), in the "Cron maintenance tasks" section.

Enabling cron

There are two distinct ways to run Backdrop's cron tasks:

1) Automated cron (internal)

The easiest way is to let Backdrop do it for you (which it does by default), using its built-in "automated cron" system.

The automated cron system is compatible with all systems, because it doesn't actually involve the system's cron daemon; it works by checking at the end of each Backdrop request to see when cron last ran. If it has been too long, the cron tasks are triggered as part of that request. The downside to this approach is that cron tasks will only run when Backdrop is processing requests. 

By default in Backdrop the page request that triggers the cron tasks will still complete while the cron tasks run in the background. This mitigates the problem of slow first-load of pages on sites with low traffic.

The background processing is an innovation over Drupal 7 where the processing and memory of running the cron tasks was added to some arbitrary unknown page request. This could slow down those requests, and also had the potential to exceed memory limits on a complex site. 

You can configure the "automated cron" via the Administration > Configuration > System > Cron (admin/config/system/cron).

Disabling "automated cron"

If you want to ensure that cron can only ever run from an external trigger, it may be desirable to disable the automated cron system.

You can disable it by setting the "Run cron every" value to "Never" (e.g., at Administration > Configuration > System > Cron (admin/config/system/cron).

2) External Cron

The second way is to create a cron job, or use some other external (to Backdrop) method of triggering its cron tasks. This is the recommended and more reliable of the two methods.  First, because it will always run on schedule. And second, it will use fewer resources because the cron processing is not added to the processing necessary for a page request.

Note that if you create an external cron job, you may want to disable the "automated cron" system entirely.

Examples

In the following example, the crontab command shown below will activate the cron tasks automatically on the hour:

0 * * * * wget -O - -q -t 1 http://www.EXAMPLE.com/core/cron.php?cron_key=KEY

 In the line above, place your own domain name in the EXAMPLE area, and replace KEY with the key displayed on the Status Report.

Native cron jobs

Cron jobs are often executed from on your own server, but outside the Backdrop website. These are called "native" cron jobs. Your hosting provider probably provides an administrative interface for setting up these cron jobs, but they can almost always be set from the command line as well. 

3rd-party cron services

There are also services like EasyCron or Cronless that provide services for executing cron jobs.