Moodle CLI Commands for Administrators
Command Line Interface (CLI) commands are invaluable for Moodle administrators, particularly for those with experience in Linux environments. CLI commands simplify tasks such as upgrades, maintenance, configuration, and troubleshooting.
This guide provides the latest information and examples for Moodle CLI commands, targeting Moodle administrators with command-line expertise.
Why Use CLI Commands in Moodle?
CLI commands are efficient and reliable for managing Moodle. They bypass the web interface, making them faster and more robust for large-scale operations or environments with restricted resources. CLI commands also integrate well with automation tools, allowing administrators to schedule tasks like backups or cron jobs.
Key Moodle CLI Commands
Below is an updated and comprehensive list of Moodle CLI commands, their purposes, and how to use them.
1. Upgrading Moodle
Upgrade your Moodle instance efficiently using the CLI. This method is ideal for administrators managing Moodle via Git. The process ensures your site is up-to-date while preserving customisations.
Command Example
sudo -u www-data php admin/cli/upgrade.php
Steps
- Enable maintenance mode:
sudo -u www-data php admin/cli/maintenance.php --enable - Pull the latest changes from Git:
git pull - Run the upgrade script:
sudo -u www-data php admin/cli/upgrade.php - Disable maintenance mode:
sudo -u www-data php admin/cli/maintenance.php --disable
2. Maintenance and Offline Mode
Maintenance Mode
Put Moodle into maintenance mode for upgrades or troubleshooting:
sudo -u www-data php admin/cli/maintenance.php --enable
Disable maintenance mode:
sudo -u www-data php admin/cli/maintenance.php --disable
Offline Mode
Create a custom offline message by placing an HTML file in the moodledata directory:
echo '<h1>Maintenance in progress</h1>' > /var/www/moodledata/climaintenance.html
3. Installing Moodle
Install Moodle interactively or non-interactively via CLI. Interactive mode prompts for input, while non-interactive mode uses parameters.
Command Examples
Interactive installation:
sudo -u www-data php admin/cli/install.php
Non-interactive installation:
sudo -u www-data php admin/cli/install.php --skip-database --lang=en
4. Backup and Restore Courses
Backup a Course
sudo -u www-data php admin/cli/backup.php --courseid=2 --destination=/path/to/backup/
Restore a Course
sudo -u www-data php admin/cli/restore.php --file=/path/to/backup.mbz --categoryid=1
5. Adjust Configuration Settings
Use the cfg.php script to view or modify Moodle configuration settings.
Command Examples
Display a configuration setting:
sudo -u www-data php admin/cli/cfg.php --name=langmenu
Set a configuration value:
sudo -u www-data php admin/cli/cfg.php --name=langmenu --set=0
6. Run the Cron Script
The cron.php script runs scheduled tasks in Moodle. It should be executed regularly.
Command Example
Set up a cron job to run every minute:
* * * * * php /var/www/moodle/admin/cli/cron.php > /dev/null 2>&1
7. Fix Course or Module Issues
Resolve sequencing issues or orphaned question categories after upgrades.
Command Examples
Fix course sequences:
sudo -u www-data php admin/cli/fix_course_sequence.php -c=* --fix
Fix orphaned question categories:
sudo -u www-data php admin/cli/fix_orphaned_question_categories.php --fix
8. Kill All User Sessions
Log out all users, including administrators:
sudo -u www-data php admin/cli/kill_all_sessions.php
9. Reset User Passwords
Reset passwords for users securely:
sudo -u www-data php admin/cli/reset_password.php
10. Build Theme CSS Cache
Force the regeneration of theme CSS:
sudo -u www-data php admin/cli/build_theme_css.php --themes=boost
11. Purge All Caches
Clear Moodle’s cache:
sudo -u www-data php admin/cli/purge_caches.php
12. Convert InnoDB Tables to Barracuda
Convert MySQL tables to the Barracuda file format for better compatibility and performance.
Command Examples
List tables requiring conversion:
sudo -u www-data php admin/cli/mysql_compressed_rows.php --list
Convert tables:
sudo -u www-data php admin/cli/mysql_compressed_rows.php --fix
13. Update Database Character Set and Collation
Convert the database to utf8mb4_unicode_ci for better multilingual support.
Command Example
sudo -u www-data php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci
14. Search and Replace in the Database
Update specific text across the database.
Command Example
sudo -u www-data php admin/cli/search_and_replace.php --search='http://oldurl.com' --replace='http://newurl.com'
15. Database Transfer
Move your Moodle database to another server or database type:
sudo -u www-data php admin/cli/dbtransfer.php --host=NEW_HOST --database=NEW_DATABASE --username=USER --password=PASS
Best Practices
- Backup First: Always back up your database and
moodledatadirectory before making major changes. - Test in a Staging Environment: Validate commands in a test instance to avoid unexpected issues.
- Automate Regular Tasks: Use cron jobs or shell scripts to streamline repetitive tasks.
Further notes...
Moodle CLI commands are powerful tools for administrators. By mastering these commands, you can manage upgrades, troubleshoot issues, and optimise performance effectively. Always ensure you’re working on the latest Moodle version to benefit from updated features and security enhancements.
