Creating and Running a Cron-job for Moodle
Getting Started!
To get started, first we need to know about the Cron job concept and the prerequisites for doing this task. let’s discuss Cron job briefly.
What is the Corn job?
The word Cron is a Greek word that means time, and Cron/Cron job is also somehow associated with time. This is a software utility that is the time-based task scheduler in Unix-like operating systems. Users who set up and maintain software environments use Cron to schedule jobs(commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates the system maintenance, backup process, administrations, reports generation for admin users, etc.
Why do we run Cron job for Moodle?
Moodle is an open-source Learning Management System(system) that needs to automate many tasks to manage the administration of LMS smoothly. In LMS setup admin needs to schedule many tasks like:
- updating reports such as quiz, admin, gradebook
- updating course and activity completion (if enabled in advanced settings)
- updating portfolio
- plagiarism checks
- updates activity modules
- updates blocks
- create the backups of courses at the time specified in the administration settings.
- updating messaging module or forum email notifications.
- unenroll students — this is done on a random basis about 20% of the time Moodle’s Cron process is triggered.
- deleting users who have not filled out their profile via the 20% random trigger
- deleting old logs are also checked 20% of the time via the 20% random trigger
- deletes old cached text
- generates new passwords for new users and notifies users
- runs authentication enrolments processes
- updates stats if enabled.
- runs blog clean-ups
- updates registrations
How many ways to run a Cron job for Moodle?
There are two ways to run the Cron job for moodle:
- The command line (CLI) Cron
- Web-based Cron
Now we will only discuss CLI Cron that we will set up using Linux commands.
Where can we find Script for tasks to be Scheduled?
The script cron.php can be found in Moodle installation directory at the following address(only if Moodle is installed on Linux)
/path to Moodle/admin/cli/cron.php
(usually path to moodle is = /var/www/html/moodle
)
Pre-requisites for running Cron-job
To start with this process one must have:
- Moodle must be installed on Unix-based systems
- User must have the root privileges
- Must have the path to
PHP
by executingwhich PHP
Let’s start!
To run the Cron job we first have to navigate to our Moodle project directory and then we will run the following command to open crontab in an editor to enter the commands to schedule the cron.php
execution.
sudo crontab -u www-data -e
This will open a text editor which looks like this:
Now we will enter the following command:* * * * * /path to PHP /path to moodle/admin/cli/cron.php >/dev/null
here use the path to PHP that is /usr/bin/php and path to moodle installation directory.
let’s break the command and understand it in detail.* * * * *
are the first five entries that define the time.
The above snippet is taken from crontab.guru which clearly explains the format of time that we can set using these five entries. as from the above image we can see that first, second, third, fourth, and fifth stands for minutes, hours, days, month and year respectively.
Now using this format we can set the scheduling time. let’s say we want our script to be executed at 10 pm every day, then our command will be:
* 22 * * * /path to PHP /path to moodle/admin/cli/cron.php >/dev/null
Note: the time is in 24 hrs format.
Next in the command is the path to PHP
followed by the path to cron.php
and terminated with EOF indicator i.e. (>/dev/null
)
Now save and close the file and we are now successful in scheduling the Cron job to be executed at 10 pm
on the daily basis.
Concluding Remarks
In this article, we learned about the Unix-based command(Cron job) that makes things easier for the Moodle administrator by automating a lot of software services on daily basis.
Any type of queries related to this article is welcome. Hope you people will find it helpful.