Dec 19, 2015

Working with Cron and hook_cron in Drupal 7

Cron is a task scheduler on the server. It allows you to customize the execution of certain tasks at a specified frequency.

Cron performs tasks ("cron jobs") that must be run regularly: clearing the cache, checking for updates and so on. But it can be used for one-time, but voluminous tasks. For example, to send a mailing list for 100 thousand letters. You can’t send them at once because mail systems can block you for spam so you can send emails in batches. For example 1000 emails per launch.

System cron vs Drupal's cron

Drupal has his own сron. This is a php file that runs by default every three hours. How does Drupal understand that it needs to be launched and how it's work? Drupal stores in the database the time of the last launch of his cron and if a visitor comes to the site after the specified time, Drupal will call hook_cron in all enabled modules.

The system cron is an operating system daemon that constantly runs and performs various tasks at specified intervals.

If you configure the system cron so that it calls cron.php (php file or Drupal's cron), visitors will not see delays due to hook_cron triggering.

If you set up a launch once an hour, then after an hour, when you open any page of the site, cron will be launched. Cron will call hook_cron functions in all enabled modules.

System cron

To configure the system cron, open the /etc/crontab file (Debian / Ubuntu) and add the following line to it:

0 */3 * * * root wget -O- http://<MYSITE_NAME>/cron.php?cron_key=<SECRET_CODE> >> /dev/null

This record will run Drupal's cron every 3 hours. A link to the launch of the cron on your website you will find on the page admin/reports/status.

For the changes to take effect, run in the console on the server:

service cron restart

Drupal's cron

Try to write hook_cron is not heavy, so that the cron had worked for 30 seconds.

Different tasks may require different execution intervals: every three hours, once a day, once a week. You can configure this with the Ultimate Cron module. It will allow each task to specify its frequency of launch.

Consider this module. Download and enable it through Drush:

drush dl ultimate_cron && drush en -y ultimate_cron

After that at admin/config/system/cron you can see see detailed information about all cron tasks (jobs):

In the Operations column you can configure the frequency of jobs. Simple — the traditional way, for example,  "every 3 hours". Crontab is a complete cron setting in the system cron format. There you can specify for example the following frequency: 10 * / 2 * * 6.7. This record means that the cron will be called "at ten minutes of every second hour on Saturdays and Sundays."

Well, the most interesting is the implementation of your own tasks for the cron.

Own task for the cron

Create your own module. File

name = Azimut7 cron.
description = Custom cron task.
version = "7.x-1.0"
project = "azimut7_cron"

We'll release hook_cron with the simplest code. Let's just add an entry to the system log.


function azimut7_cron_cron() {
  watchdog('cron', 'My cron message');

Do not forget to enable the module:

Now you can run cron through admin panel. Go to admin/config/system/cron and click Run cron.

After that, in the Recent log messages at admin/reports/dblog you will see your entry (if it is not there — check if the Syslog module is enabled).