Module 7: Power User & CLI

Command Line Administration, Cron, and Advanced Configuration

1. The Moodle CLI

Moodle provides powerful CLI tools in admin/cli/. These are essential for system administrators and developers.

Common Commands

# Run Cron (Essential)
php admin/cli/cron.php

# Clear Caches (The "Turn it off and on again" of Moodle)
php admin/cli/purge_caches.php

# Install Moodle
php admin/cli/install.php --lang=en --wwwroot=... --dataroot=...

# Maintenance Mode
php admin/cli/maintenance.php --enable
php admin/cli/maintenance.php --disable

# Database Transfer (Migrate DBs)
php admin/tool/dbtransfer/cli/migrate.php

2. Scheduled Tasks (Cron)

Moodle's heartbeat is the Cron. It handles emails, forum posts, course completions, and cleanup.

// Defining a task in db/tasks.php
$tasks = [
    [
        'classname' => 'local_myplugin\task\cleanup_task',
        'blocking' => 0,
        'minute' => '0',
        'hour' => '2', // Run at 2 AM
        'day' => '*',
        'month' => '*',
        'dayofweek' => '*',
    ],
];

3. config.php Tricks

The config.php file can override database settings to force specific behaviors.

// Force Debugging (Don't do this in Prod!)
$CFG->debug = 32767; // DEBUG_DEVELOPER
$CFG->debugdisplay = 1;

// Force Theme
$CFG->theme = 'boost';

// Prevent Plugin Installation via UI
$CFG->disableupdateautodeploy = true;

// Performance: Session Handling (Redis)
$CFG->session_handler_class = '\core\session\redis';
$CFG->session_redis_host = '127.0.0.1';

4. Ad-hoc Tasks

Unlike scheduled tasks which run on a schedule, Ad-hoc tasks are "fire and forget" background jobs. They are perfect for heavy processing triggered by a user action (e.g., generating a certificate, processing a CSV upload).

// 1. Create a task class extending \core\task\adhoc_task
namespace local_myplugin\task;

class generate_report extends \core\task\adhoc_task {
    public function execute() {
        $data = $this->get_custom_data();
        // Long running process here...
        mtrace("Report generated for user: " . $data->userid);
    }
}

// 2. Queue the task from anywhere in your code
$task = new \local_myplugin\task\generate_report();
$task->set_custom_data(['userid' => $USER->id]);
\core\task\manager::queue_adhoc_task($task);

5. CLI Backup & Restore

For large courses, the web interface often times out. Use the CLI tools for reliable backups and migrations.

# Backup a specific course
php admin/cli/backup.php --courseid=10 --destination=/var/www/backups/

# Restore a course
php admin/cli/restore.php --file=/var/www/backups/backup.mbz --categoryid=1

# Backup the entire site (Code + DB + Moodledata)
# Note: Moodle doesn't have a single command for this. 
# You typically script: tar moodledata, mysqldump db, tar code.

6. Performance Tuning

Optimizing Moodle for high concurrency involves several layers of caching.

// config.php: Redis for MUC (Application Cache)
$CFG->session_handler_class = '\core\session\redis';
$CFG->session_redis_host = '127.0.0.1';
$CFG->session_redis_prefix = 'moodle_prod_sess_';
$CFG->session_redis_acquire_lock_timeout = 120;
$CFG->session_redis_lock_expire = 7200;