Looking for an assistant to handle all your routine Drupal tasks including checking for updates and clean-ups, without any manual intervention? We'd like to introduce you to Cron Jobs!

Now let’s dive into the world of this behind-the-scenes helper, specifically in the context of task automation within Drupal. In this blog, we will delve into the fascinating realm of automation, explore the fundamental concept of cron jobs, provide an overview of cron jobs in Drupal, and discuss various essential aspects of crafting your first custom Drupal cron job.


What is Automation

Automation is the practice of utilizing technology, machinery, or software to perform tasks, processes, or operations with minimal or no human intervention. The primary objective of automation is to streamline and optimize workflows, reduce manual effort, improve efficiency, enhance accuracy, and increase productivity. In simpler terms, automation entails using technology to minimize the human effort required for task execution.

As technology continues to advance, we can expect to see automated machines, technology, and software playing a more significant role in many industries and everyday tasks.

Now, keeping all of these in mind and asking ourselves how we can automate something, we move on to the next topic.

What are Cron Jobs?

The term "cron" is frequently encountered in the domain of computers, especially within operating systems like Linux. It serves as a tool for scheduling tasks based on time. In Unix systems, such as Linux, "cron" is a scheduling utility. A "cron job" is an automated task scheduled using the "cron" utility.

Cron is a daemon – a background process that handles non-interactive tasks. In Unix-like operating systems, a daemon is a component, or a type of program, that silently operates in the background without requiring human interaction. It's like having a robot that wakes up and starts working when it hears a specific sound or when something specific happens.

Cron jobs provide users with a powerful tool to automate system maintenance, monitor disk space, and manage backups at regular intervals.

While cron job scheduling is widely favored by system administrators, it also offers significant benefits to web developers. It functions as a built-in alarm clock for your website tasks, ensuring that you can automate routine actions with ease.

Cron is typically pre-installed on the system. However, in cases where it's not available, you can easily install it using the following commands:

  • Update the package repository
sudo apt update
  • Install cron
sudo apt install cron
  • Enable the cron service to ensure it runs as expected
sudo systemctl enable cron


The question arises: How can we activate cron or configure a customized cron to meet our specific needs? In this scenario, crontab comes to the rescue.

Crontab

A crontab is like a to-do list of tasks you want your computer to do at specific times, and it's also the name of the command you use to create and manage that list. Crontab stands for “cron table”.

Each user in Linux has their own crontab, which may be used to schedule jobs that will be executed as that user. You can view, and update the crontab file using the crontab command.

Example:

  • To update the crontab file

crontab -e
  • To list down or show the commands stored in the crontab file

crontab -l

Cron Expression

In simple terms, a cron expression in Linux is like setting an alarm for your computer to do specific tasks regularly. You have five fields to play with: minutes, hours, days of the month, months, and days of the week. By adjusting these fields, you can tell your computer exactly when to do things. There are also special characters like asterisks and slashes that let you be flexible with your schedule. For instance, if you set a cron expression like "0 * * * *," it means your computer will perform a task at the beginning of every hour. It's a handy tool that makes your computer do routine jobs automatically, like maintenance, backups, and running scripts.

A job is represented by each line of a crontab file, which looks like this

SYNTAX:

# ┌────── minute (0 - 59)
# │ ┌────── hour (0 - 23)
# │ │ ┌────── day of the month (1 - 31)
# │ │ │ ┌────── month (1 - 12)
# │ │ │ │ ┌────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# *  *  *  *  *  command_to_execute

Examples:

  • At every 1 minute: * * * * *
  • At every 2 minute:  * /2 *  *  *  *
  • At every 15 minute: * /15  *  *  *  *
  • At every 1 hour: 0  *  *  *  *
  • At every 3 hours: 0  * /3  *  *  *
  • At every 8 hours: 0  * /8  *  *  *
  • Everyday (At 00:00):  0  0  *  *  *

Hour range:

  • Every hour between 11 am to 2 pm: 0  11-14  *  *  *

Thanks to crontab Guru which is a simple editor for cron schedule expressions. For more examples, you can refer to this page.

Drupal Cron Job Overview

Drupal has a handy feature called Automated Cron. The Drupal automated cron job is an automatic task scheduler that handles regular website upkeep, including tasks like optimizing the database and clearing the cache, all at predetermined time intervals. It guarantees the website's smooth operation and data precision, minimizing the necessity for manual involvement.

Some key features of automated cron:

  • Scheduled Maintenance Tasks:

In simple terms, Drupal's automated cron is like a behind-the-scenes helper that does regular chores for your website. It cleans up the website's storage, checks for important updates, and takes care of other routine jobs without bothering you. This helps keep your website running smoothly without you having to do these tasks manually.

  • Configurable Frequency:

If you're in charge of a Drupal website, you can decide how often the automated cron job should do its work. By default, it does its tasks every three hours, but you can change that to better suit your website's requirements. You can set this schedule by going into the Drupal admin settings, and it's kind of like adjusting the timing on a sprinkler system for your garden.

  • Background Process:

Think of automated cron in Drupal like a silent worker in the background. It gets things done without disturbing the part of your website that visitors see. It doesn't need outside tools to know when to do its tasks; it uses its internal clock, much like an alarm clock that wakes you up at a specific time without needing help from another device.

Limitations of Drupal cron:

  • Simultaneous Execution of hook_cron Modules and Resource Challenges:

When several modules use the hook_cron function, they all run simultaneously, either in alphabetical order or based on the weight assigned to each module. This can make a webpage request more resource-intensive all at once and has the potential to exceed memory limits on a complex site.

  • Handling Errors in Cron Tasks and Identifying Culprits for Smooth Execution:

If there's an error in one cron task, it can halt the entire cron process, preventing the execution of the remaining tasks until the issue is fixed. It's also challenging to identify which task is causing the problem.

  • Optimizing Cron Execution by Analyzing Task Durations:

To improve the efficiency of cron execution and to optimize the cron run time. It's important to examine which cron tasks consumed more time during their run.

Now to overcome all these limitations, Drupal provides us with modules like Ultimate Cron, Super Cron (abandoned module), Elysia Cron (Drupal 7), etc.. All these modules are one or the same with more added features. Now Super Cron is unsupported, the module is abandoned due to some security issues. The module is no longer developed by the maintainer whereas Elysia cron is for Drupal 7.

How does Ultimate Cron Help?

  • Parallel Execution of Cron Tasks: Error Isolation and Independence:

Tasks are no longer executed sequentially; they now run in parallel. This means that if one cron task encounters an error, it won't affect the independent execution of other tasks.

  • Individual Configuration for Cron Jobs with Differing Schedules:

You can configure each cron job separately. For example, you can set up your cron job to run every hour and another cron job (cron job B) to run every two hours.

  • Independent Logging for Each Cron Job:

Each has separate logs.

Let's install the Ultimate cron module and create our first custom cron job.

So, to install the module you can visit https://www.drupal.org/project/ultimate_cron and download the module using composer. Use this command:

composer require 'drupal/ultimate_cron:^2.0@alpha'

This will install the Ultimate cron module, the next step is to enable it. You can enable the module via the site or by drush command. Use this drush command to enable the module:

drush en ultimate_cron

After installing Ultimate cron, we get some predefined cron jobs that the module provides, for example, cleanups (cache, temp files), removing expired log messages, and history deletion.

Path: xyz.domain.name/admin/config/system/cron/jobs

How to Configure Custom Ultimate Cron Job

  1. Create a config/install directory in the root of your custom module’s directory.
  2. Make a new file called ultimate_cron.job.jobname.yml (replace jobname with the name of your job).

In our case we are creating a custom cron job to publish all the unpublished article contents, therefore let's name our job as publish_article_job.

3. Next step is to edit the ultimate_cron.job.publish_article_job.yml file.

Inside ultimate_cron.job.publish_article_job.yml, we’ll add the details which will include a callback function, cron job ID, and rules for scheduling the task.

langcode: en
status: true
dependencies:
 module:
   - custom_cron_example
title: 'Custom job for publishing articless'
id: publish_article_job
module: custom_cron_example
callback: custom_cron_example_publish_article_job
scheduler:
 id: simple
 configuration:
   rules:
     - '* * * * *'

4. Once editing the configuration file, we will now define a callback function inside the custom_cron_example.module file.

<?php
use Drupal\node\Entity\Node;
/**
* Custom ultimate cron callback function.
*/
function custom_cron_example_publish_article_job() {
 $message = "Voila ! Ultimate cron works.";
 \Drupal::logger("custom_cron_example")->notice($message);
 // Query to fetch all article unpublished nodes.
 $query = \Drupal::entityQuery('node');
 $query->condition('status', 0);
 $query->condition('type', 'article');
 $entity_ids = $query->accessCheck(FALSE)->execute();
 foreach ($entity_ids as $entity_id) {
   $node = Node::load($entity_id);
   $node->setPublished(TRUE)->save();
 }
}

Once all steps have been completed, it's time to activate the module in order to ensure that the new configurations we've created are applied to the website. Once the module is enabled, you will be able to view your custom cron job on the cron job dashboard.

 

Now this cron job will run every 1 minute since we added the cron expression as * * * * *.

You can alter the rule as per your requirement. Feel free to use crontab guru for references. I believe there’s no harm in that ;)


Results:

  • Before the cron run, the article was unpublished.


 

  • Once the scheduled task (cron) is completed, the article gets published on the website. Additionally, there's a record in the database log that displays our unique message, which we included in the custom callback function we created.


 

Enabling Cron using External Calls

  1. By using crontab on the server.
  2. External cron job services like easycron or cronless. You can configure the time as per your requirement. 

    Easycron

Cronless

It may be beneficial to disable the automated cron system for performance reasons or to ensure that cron can only ever run from an external trigger.

To disable the automated cron:

Change the value of "Run cron every" to "Never."
                    (OR)

Add the following line to your settings.php: (so that no other user can enable it).

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

Final Thoughts

In today's world, Drupal's cron job system remains essential for automated site maintenance. However, its design limitations and potential resource impact are better addressed with modern alternatives like scheduled tasks in server or external cron services, ensuring smoother website performance and management. Automated technology and machines are fascinating aspects of the future that hold the potential to revolutionize various aspects of our lives. However, adopting automation will also require careful consideration of its impact on the workforce and society as a whole

Is your online presence ready for an upgrade? Let's discuss how Specbee can be your strategic partner in Drupal development.

 

Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US