Scheduled Tasks
Moodle uses a task scheduling system to run background operations at specified intervals. The scheduled task system replaced the older event-based cron system and provides granular control over when and how tasks execute.
Viewing Scheduled Tasks
Navigate to Site administration > Server > Scheduled tasks to see all registered tasks. The table displays:
- Task name: The human-readable name and the PHP class name
- Component: The plugin or core component that owns the task
- Schedule: When the task is configured to run (minute, hour, day, month, day of week)
- Last run: Timestamp of the last successful execution
- Next run: When the task is scheduled to run next
- Running: Whether the task is currently executing
- Fail delay: Exponential backoff delay if the task has been failing
Task Schedule Format
Tasks use a cron-like schedule format with five fields:
Minute (0-59)
Hour (0-23)
Day of Month (1-31)
Month (1-12)
Day of Week (0-6, where 0 = Sunday)
Special values:
*— Every value (e.g., every minute, every hour)*/5— Every 5th value (e.g., every 5 minutes)1,15— Specific values (1st and 15th minute)R— Random value within the range (Moodle-specific; useful for distributing load)
Editing Task Schedules
Click the edit icon next to a task to modify its schedule. You can change the minute, hour, day, month, and day of week fields. You can also:
- Disable the task: Prevents the task from running via cron
- Reset to default: Restores the task to its original schedule
Common Scheduled Tasks
Key tasks that run in a typical Moodle installation:
core asksend_new_user_passwords_task— Sends password notifications to new userscore asksend_failed_login_notifications_task— Sends failed login alertscore askcron_task— Legacy cron task for backward compatibilitycore asksession_cleanup_task— Cleans up expired sessionscore askfile_trash_cleanup_task— Removes deleted files from trashcore askcache_cleanup_task— Cleans up expired cache entriescore askautomated_backup_task— Runs automated course backupscore askstats_cron_task— Processes site statisticscore askcompletion_regular_task— Recalculates activity completioncore askadges_cron_task— Processes badge criteria and awardscore askgrade_cron_task— Recalculates gradesmod_forum asksend_user_digests— Sends forum digest emails
Running Tasks via CLI
Execute a specific task immediately from the command line:
# Run a specific task
sudo -u www-data php admin/cli/scheduled_task.php
--execute=\core\task\automated_backup_task
# List all tasks and their status
sudo -u www-data php admin/cli/scheduled_task.php --list
# Show the status of a specific task
sudo -u www-data php admin/cli/scheduled_task.php
--showsql --execute=\core\task\stats_cron_task
Ad Hoc Tasks
In addition to scheduled tasks, Moodle uses ad hoc tasks for one-time operations that need to run in the background. These include:
- Sending individual email notifications
- Processing assignment submission events
- Course backup operations
- Content indexing for global search
Ad hoc tasks are stored in the mdl_task_adhoc table and processed during each cron run. You can view pending ad hoc tasks with:
SELECT classname, COUNT(*) as pending
FROM mdl_task_adhoc
GROUP BY classname
ORDER BY pending DESC;
Task Logs
Moodle logs task execution results. View task logs at Site administration > Server > Task logs. The logs show:
- Task name and type (scheduled or ad hoc)
- Start and end time
- Duration
- Result (success or failure)
- Output and error messages
- Database reads and writes
Troubleshooting Task Issues
- Task stuck "running": If a task shows as running for an unusually long time, it may have crashed. Reset the running flag in the database or wait for the lock to expire.
- Fail delay increasing: When a task fails repeatedly, Moodle applies an exponential backoff delay (1 min, 2 min, 4 min, etc., up to 24 hours). Fix the underlying issue, then reset the fail delay by editing the task.
- Tasks not running at all: Verify that cron is properly configured and running every minute.