Cron Setup

Cron Setup

The cron system is a critical component of Moodle. It processes scheduled tasks that keep your site running properly, including sending email notifications, running automated backups, cleaning up temporary files, processing assignment submissions, calculating grades, and much more.

Why Cron is Essential

Without a properly running cron job, many Moodle features will not work correctly:

  • Email notifications for forum posts, assignment submissions, and messages will not be sent
  • Automated course backups will not run
  • Calendar event reminders will not be triggered
  • Activity completion will not be automatically recalculated
  • Course completion criteria will not be evaluated
  • RSS feeds will not be updated
  • Recycling bin items will not be cleaned up
  • Statistics will not be processed

CLI Cron vs Web Cron

Moodle supports two methods of triggering cron:

CLI Cron (Recommended)

Running cron via the command line is the recommended approach because it runs as the web server user with no time limits:

# Edit the crontab for the web server user
sudo crontab -u www-data -e

# Add this line to run cron every minute
* * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php > /dev/null 2>&1

Alternatively, if you want to log cron output:

* * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php >> /var/log/moodle_cron.log 2>&1

Web-Based Cron

If CLI access is not available (e.g., some shared hosting), you can use the web-based cron URL:

https://yourmoodle.example.com/admin/cron.php

Set this up with a cURL command in crontab or an external cron service:

* * * * * /usr/bin/curl -s https://yourmoodle.example.com/admin/cron.php?password=YOURCRONPASSWORD > /dev/null 2>&1

Security note: If using web-based cron, you should set a cron password at Site administration > Security > Site security settings > Cron password for remote access. Alternatively, restrict cron to CLI only by enabling Cron execution via CLI only in the same settings page.

Recommended Cron Frequency

Moodle recommends running the cron job every minute. This is the default expectation in Moodle 4.x, as the scheduled task system is designed to manage its own timing. Running cron every minute ensures that tasks scheduled to run at specific times (e.g., every 5 minutes, every hour) execute on time.

Running cron less frequently (e.g., every 5 or 15 minutes) may delay email notifications and other time-sensitive operations.

Cron Lock and Concurrent Execution

Moodle uses a locking mechanism to prevent multiple cron processes from running simultaneously. If a cron process is already running when the next trigger occurs, the new process will detect the lock and exit gracefully. The default lock timeout is 15 minutes.

If cron appears stuck (e.g., a task has been running for an abnormally long time), you can:

  1. Check running processes: ps aux | grep cron.php
  2. Check the scheduled tasks page: Site administration > Server > Scheduled tasks shows the last run time and status of each task
  3. Kill a stuck cron process if necessary and let the next cron trigger clean up the lock
  4. Manually reset task locks through the database if needed:
    DELETE FROM mdl_lock_db WHERE resourcekey LIKE '%cron%';
    

Running Individual Tasks

You can run a specific scheduled task manually via the CLI:

sudo -u www-data php admin/cli/scheduled_task.php --execute=\core\task\send_new_user_passwords_task

To list all scheduled tasks and their status:

sudo -u www-data php admin/cli/scheduled_task.php --list

Verifying Cron is Running

Check if cron is running properly by:

  • Visiting Site administration > Server > Scheduled tasks — if the "Last run" column shows recent timestamps, cron is working
  • Checking for the warning message on the admin dashboard about cron not running
  • Verifying email notifications are being sent (forum subscriptions are a good test)
  • Checking the cron log in Moodledata or system logs

Systemd Timer Alternative

On modern Linux systems, you can use a systemd timer instead of crontab:

# /etc/systemd/system/moodle-cron.service
[Unit]
Description=Moodle Cron Service

[Service]
Type=oneshot
User=www-data
ExecStart=/usr/bin/php /var/www/moodle/admin/cli/cron.php
# /etc/systemd/system/moodle-cron.timer
[Unit]
Description=Run Moodle cron every minute

[Timer]
OnCalendar=*-*-* *:*:00
Persistent=true

[Install]
WantedBy=timers.target

Enable with: sudo systemctl enable --now moodle-cron.timer

¿Le ha resultado útil este artículo?

  • Installing Moodle

    Installing Moodle This guide covers the complete installation process for Moodle 4.x/5.x on a web se...
  • Upgrading Moodle

    Upgrading Moodle Keeping your Moodle installation up to date is essential for security patches, bug ...
  • Admin Quick Guide

    Admin Quick Guide This guide walks you through the essential first steps after installing Moodle. Wh...
  • Site Home Settings

    Site Home Settings The site home (also known as the front page) is the landing page of your Moodle i...
  • Language Settings

    Language Settings Moodle supports over 100 languages and provides a comprehensive internationalizati...