Note: You are currently viewing documentation for Moodle 3.1. Up-to-date documentation for the latest stable version of Moodle is probably available here: Administration via command line.

Administration via command line: Difference between revisions

From MoodleDocs
No edit summary
 
(41 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{Installing Moodle}}
{{Installing Moodle}}
==Running CLI scripts==
If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the <code>admin/cli/*</code> folder. Other plugins provide their CLI functionality via scripts in their own cli folder. For example, the enrol_db sync script is located in <code>enrol/db/cli/</code>.
If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the <code>admin/cli/*</code> folder. Other plugins provide their CLI functionality via scripts in their own cli folder. For example, the enrol_db sync script is located in <code>enrol/db/cli/</code>.


Line 11: Line 12:
     $ sudo -u apache /usr/bin/php admin/cli/install.php --help
     $ sudo -u apache /usr/bin/php admin/cli/install.php --help


== Upgrading via command line ==
{{Note|These scripts are supposed to be run under the identity of the web server user. Examples on this page use the <tt>apache</tt> user for illustration. The particular value depends on your OS distribution and local set-up. Typical values may be <tt>apache</tt>, <tt>www-data</tt> or <tt>httpd</tt>.}}
 
== Upgrading ==


Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)
Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)
Line 25: Line 28:
     $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable
     $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable


== Installation via command line ==
== Installation ==


Since version 2.0, Moodle can be installed from the command line. There are two modes of installation. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.
There are two modes of installing Moodle from the command line. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.


     $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs
     $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs
If required, the database install may be skipped, with just config.php populated.
  $ sudo -u apache /usr/bin/php admin/cli/install.php --skip-database


== Maintenance mode ==
== Maintenance mode ==
Line 37: Line 44:
     $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
     $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable


To turn the maintenance mode off, just execute the same script with --disable parameter.
To turn maintenance mode off, just execute the same script with --disable parameter.


== Offline mode ==
== Offline mode ==
Line 58: Line 65:
</code>
</code>


These defaults are used during install, upgrade and are also displayed as defaults at the Site administration pages.
These defaults are used during install, upgrade and are also displayed as defaults on Site administration pages.


== Reset user password ==
== Reset user password ==


If you happen to forget your admin password (or you want to set a password for any other user of your Moodle system), you can use reset_password.php script. The script sets the correctly salted password for the given user.
If you happen to forget your admin password (or you want to set a password for any other user on the site), you can use reset_password.php script. The script sets the correctly salted password for the given user.


     $ sudo -u apache /usr/bin/php admin/cli/reset_password.php
     $ sudo -u apache /usr/bin/php admin/cli/reset_password.php
Line 71: Line 78:


     $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
     $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
==Converting InnoDB tables to Barracuda==
Sites using MySQL with database tables using Antelope as the file format are recommended to convert the tables to the Barracuda file format.
This is because tables using Antelope as the file format cannot handle more than 10 text columns. This file formats only supports ''compact'' and ''redundant'' row formats for backward compatibility reasons. This may cause a problem on larger sites when restoring a course, in which case the following error will be displayed:
Row size too large (>8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help.
Barracuda is the newest innoDB file format. In addition to supporting ''compact'' and ''redundant'' row formats, Barracuda also supports ''compressed'' and ''dynamic'' row formats.
However, converting tables to Barracuda is only recommended, and not required, since not all MySQL users are affected. (It may only be a problem for larger sites.)
===Tool for converting tables===
A command line tool is included in Moodle for converting tables to Barracuda.
To view tables requiring conversion, use the list option:
$ php admin/cli/mysql_compressed_rows.php --list
Here is an example output:
mdl_data                            Compact    (needs fixing)
mdl_data_fields                    Compact    (needs fixing)
mdl_enrol_paypal                    Compact    (needs fixing)
To proceed with the conversion, run the command using the fix option:
$ php admin/cli/mysql_compressed_rows.php --fix
Successful table conversion will be reported in the output, for example:
mdl_data                  ... Compressed
mdl_data_fields            ... Compressed
mdl_enrol_paypal          ... Compressed
Please note that the commands must be executed on your moodle directory. Once tables are fixed, the warning message will no longer be displayed.
For further information on InnoDB file formats see the [http://dev.mysql.com/doc/innodb/1.1/en/glossary.html#glos_antelope MySQL InnoDB glossary - Antelope] and the [http://dev.mysql.com/doc/innodb/1.1/en/glossary.html#glos_barracuda MySQL InnoDB glossary - Barracuda].
If you get errors due to having insufficient privileges to run these commands (this is quite likely) then the easiest solution is to generate the required SQL commands using,
$ php admin/cli/mysql_compressed_rows.php --showsql
You can then copy the generated SQL into your mysql client running as the 'root' user.


== Running cron via command line ==
== Running cron via command line ==
Line 76: Line 129:
In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.
In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.


==Database transfer==
== Scheduled tasks ==


A command line script for [[Database transfer]] may be found in ''admin/tool/dbtransfer/cli/migrate.php''.
Scheduled tasks are automatically run by the cron script, but the specific tasks which run on each cron iteration are determined by the scheduled tasks configuration. It is possible to override the scheduled tasks configuration and run a single scheduled task immediately using the admin/tool/task/cli/schedule_task.php script.  


==Update user pictures via command line==
This script accepts the following arguments:


  http://moodle.org/pluginfile.php/143/mod_forum/attachment/926929/updatepics.php
--list - list all the known scheduled tasks. The tasks are listed by the class name used to run the task. This class name is required as the argument to the next option in order to run a specific task immediately.
Tested for 2.3.1+


Place this file in /admin/cli/updatepics.php
--execute=<task> - Runs a single scheduled task immediately - regardless of scheduling settings. This will even run disabled tasks. Tasks will still use locking to prevent concurrent execution of the same task - even on clusters. The format of the <task> argument must be the same as returned by the --list option above.


How to use:
'''Note:''' You must escape the "\" with an extra \ when using the --execute command. Take the following for example:
Run with "dir" option to specify which directory contains the files.


  .../admin/cli/updatepics.php --dir=PATH_TO_DIR
<pre>php schedule_task.php --list</pre>


* File names must be identical to usernames
will return something like:


==Manage MOD's,Blocks via command line==
<pre>
== List of scheduled tasks (http://yourserver.com/moodle) ==
\enrol_imsenterprise\task\cron_task                10 * * * * *      ASAP
\logstore_legacy\task\cleanup_task                * 5 * * * *      ASAP
\logstore_standard\task\cleanup_task              * 4 * * * *      Wednesday, November 12, 2014, 4:35 AM
\mod_forum\task\cron_task                          * * * * * *      ASAP
\core\task\automated_backup_task                  50 * * * * *      ASAP


Here are two scripts to help you manage your plugins.
...
</pre>


  http://tracker.moodle.org/browse/MDL-35736
To run the first task in that list, you would execute:


How to use "manage_blocks.php":
<pre>php schedule_task.php --execute=\\enrol_imsenterprise\\task\\cron_task</pre>
place in /admin/cli


hide:
Windows servers do not require the extra escapes. Thus the command is simply
../admin/cli/manage_blocks.php --name=tag_youtube --hide
<pre>php schedule_task.php --execute=\enrol_imsenterprise\task\cron_task</pre>


show:
==Database transfer==
../admin/cli/manage_blocks.php --name=tag_youtube --show


delete:
A command line script for [[Database transfer]] may be found in ''admin/tool/dbtransfer/cli/migrate.php''.
../admin/cli/manage_blocks.php --name=tag_youtube --delete
 
protect:
../admin/cli/manage_blocks.php --name=tag_youtube --protect


unprotect:
==Purge caches==
../admin/cli/manage_blocks.php --name=tag_youtube --unprotect


(obviously - replace "tag_youtube" with the blockthat you want to remove...)
You can purge caches using this script:


How to use "manage_mods.php":
  php admin/cli/purge_caches.php


hide:
==Fix course / module sequences==
../admin/cli/manage_blocks.php --name=game --hide


show:
In rare cases (such as after upgrading from a very old version of Moodle), the course / section / module sequence data can be out of sync. This can cause various problems for affected courses, such as sections not appearing, backups failing, pages not displaying etc. There is a specific check to check for errors caused by this problem, and to fix the data in the database if they are found. To run this script please use the command below:
../admin/cli/manage_blocks.php --name=game --show


delete:
  php admin/cli/fix_course_sequence.php -c=* --fix
../admin/cli/manage_blocks.php --name=game --delete


*Important!
This will check every course in Moodle and report which ones had errors and were fixed.
this script does not delete the relevant folder in /moodle/mod or /moodle/blocks


you have to delete it yourself !
==Fix orphaned question categories==


==Update language pack via CLI==
When a quiz is created, a new question category for the quiz is automatically created. In versions of Moodle prior to 2.9.1, if the quiz is deleted, the question category and any questions in the category remain in database. These orphaned question categories may be fixed by running the admin/cli/fix_orphaned_question_categories.php script with the --fix option.


  http://tracker.moodle.org/browse/MDL-35735
==Search and replace text==


Place attached file in /admin/cli
This script can be used to search and replace text throughout the whole database. Use carefully and backup first always. More info in [[Search and replace tool]].
and simply run it, without any command-line arguments :)


==ReSort course list via CLI==
php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost


  http://tracker.moodle.org/browse/MDL-36237
Options:
--search=STRING      String to search for.
--replace=STRING      String to replace with.
--shorten            Shorten result if necessary.
--non-interactive    Perform the replacement without confirming.
-h, --help            Print out this help.


Place attached file in /admin/cli
==See also==
and simply run it, without any command-line arguments :)


I import courses every night from an external system, and run this script afterwards
* MDL-35736 - Manage plugins via command line
* MDL-36237 - Resort course list via CLI
* [http://moosh-online.com/ MOOSH] - MOOdle SHell. It is a commandline tool that will allow you to perform most common Moodle tasks.


[[fr:Administration en ligne de commande]]
[[fr:Administration en ligne de commande]]
[[de:Administration über Kommandozeile]]
[[de:Administration über Kommandozeile]]
[[ja:コマンドライン経由の管理]]
[[ja:コマンドライン経由の管理]]
[[es:Administración por línea de comando]]

Latest revision as of 13:15, 3 April 2017

Running CLI scripts

If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the admin/cli/* folder. Other plugins provide their CLI functionality via scripts in their own cli folder. For example, the enrol_db sync script is located in enrol/db/cli/.

To avoid problems with access control, you should run them as the owner of the web server process. It is especially important for CLI installation and upgrade as they create new files in moodledata directory and the web server has to have write access to them. In Linux distributions, the user that runs the web server is usually apache or wwrun or httpd or something similar. As a root, you will probably want to execute Moodle CLI scripts like this:

   $ cd /path/to/your/moodle/dir
   $ sudo -u apache /usr/bin/php admin/cli/somescript.php --params

Most of the scripts accept common --help (or -h) parameter to display the full usage information, for example:

   $ sudo -u apache /usr/bin/php admin/cli/install.php --help
Note: These scripts are supposed to be run under the identity of the web server user. Examples on this page use the apache user for illustration. The particular value depends on your OS distribution and local set-up. Typical values may be apache, www-data or httpd.


Upgrading

Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)

   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php

Upgrading via command line is a very comfortable way of Moodle upgrade if you use Git checkout of the Moodle source code (see Git for Administrators). See the following procedure how to upgrade your site within several seconds to the most recent version while preserving your eventual local customizations tracked in git repository:

   $ cd /var/www/sites/moodle/htdocs/
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
   $ git pull
   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable

Installation

There are two modes of installing Moodle from the command line. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.

   $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs

If required, the database install may be skipped, with just config.php populated.

  $ sudo -u apache /usr/bin/php admin/cli/install.php --skip-database

Maintenance mode

To switch your site into the maintenance mode via CLI, you can use

   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable

To turn maintenance mode off, just execute the same script with --disable parameter.

Offline mode

In some situations, you may want to switch your Moodle site into offline mode so that it is not accessible via the web but you can not stop the web server completely (typically because there are other web pages and applications running there). If a file called climaintenance.html exists in the root folder of moodledata directory, Moodle will automatically display the contents of that file instead of any other page.

   $ cd /var/www/sites/moodle/moodledata/
   $ echo '<h1>Sorry, maintenance in progress</h1>' > climaintenance.html

You can prepare a nice formatted HTML page to inform your users about the server being down and keep in the moodledata directory under a name like climaintenance.off and rename it to the climaintenance.html if needed.

Custom site defaults

During the install and upgrade via CLI, Moodle sets the administration variables to the default values. You can use different defaults. See MDL-17850 for details. Shortly, all you need to do is to add a file local/defaults.php into your Moodle installation. The format of the file is like

<?php $defaults['pluginname']['settingname'] = 'settingvalue'; // for plugins $defaults['moodle']['settingname'] = 'settingvalue'; // for core settings

These defaults are used during install, upgrade and are also displayed as defaults on Site administration pages.

Reset user password

If you happen to forget your admin password (or you want to set a password for any other user on the site), you can use reset_password.php script. The script sets the correctly salted password for the given user.

   $ sudo -u apache /usr/bin/php admin/cli/reset_password.php

MySQL storage engine conversion

If you run your Moodle site with MySQL database backend and use the default MyISAM as the storage engine for your tables, you may want to convert them to use some more reliable engine like InnoDB (actually, you should want to switch to PostgreSQL ;-) anyway).

   $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB

Converting InnoDB tables to Barracuda

Sites using MySQL with database tables using Antelope as the file format are recommended to convert the tables to the Barracuda file format.

This is because tables using Antelope as the file format cannot handle more than 10 text columns. This file formats only supports compact and redundant row formats for backward compatibility reasons. This may cause a problem on larger sites when restoring a course, in which case the following error will be displayed:

Row size too large (>8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help.

Barracuda is the newest innoDB file format. In addition to supporting compact and redundant row formats, Barracuda also supports compressed and dynamic row formats.

However, converting tables to Barracuda is only recommended, and not required, since not all MySQL users are affected. (It may only be a problem for larger sites.)

Tool for converting tables

A command line tool is included in Moodle for converting tables to Barracuda.

To view tables requiring conversion, use the list option:

$ php admin/cli/mysql_compressed_rows.php --list

Here is an example output:

mdl_data                            Compact     (needs fixing) 
mdl_data_fields                     Compact     (needs fixing)
mdl_enrol_paypal                    Compact     (needs fixing)

To proceed with the conversion, run the command using the fix option:

$ php admin/cli/mysql_compressed_rows.php --fix

Successful table conversion will be reported in the output, for example:

mdl_data                   ... Compressed
mdl_data_fields            ... Compressed
mdl_enrol_paypal           ... Compressed

Please note that the commands must be executed on your moodle directory. Once tables are fixed, the warning message will no longer be displayed.

For further information on InnoDB file formats see the MySQL InnoDB glossary - Antelope and the MySQL InnoDB glossary - Barracuda.

If you get errors due to having insufficient privileges to run these commands (this is quite likely) then the easiest solution is to generate the required SQL commands using,

$ php admin/cli/mysql_compressed_rows.php --showsql

You can then copy the generated SQL into your mysql client running as the 'root' user.

Running cron via command line

In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.

Scheduled tasks

Scheduled tasks are automatically run by the cron script, but the specific tasks which run on each cron iteration are determined by the scheduled tasks configuration. It is possible to override the scheduled tasks configuration and run a single scheduled task immediately using the admin/tool/task/cli/schedule_task.php script.

This script accepts the following arguments:

--list - list all the known scheduled tasks. The tasks are listed by the class name used to run the task. This class name is required as the argument to the next option in order to run a specific task immediately.
--execute=<task> - Runs a single scheduled task immediately - regardless of scheduling settings. This will even run disabled tasks. Tasks will still use locking to prevent concurrent execution of the same task - even on clusters. The format of the <task> argument must be the same as returned by the --list option above.

Note: You must escape the "\" with an extra \ when using the --execute command. Take the following for example:

php schedule_task.php --list

will return something like:

== List of scheduled tasks (http://yourserver.com/moodle) ==
\enrol_imsenterprise\task\cron_task                10 * * * * *      ASAP
\logstore_legacy\task\cleanup_task                 * 5 * * * *       ASAP
\logstore_standard\task\cleanup_task               * 4 * * * *       Wednesday, November 12, 2014, 4:35 AM
\mod_forum\task\cron_task                          * * * * * *       ASAP
\core\task\automated_backup_task                   50 * * * * *      ASAP

...

To run the first task in that list, you would execute:

php schedule_task.php --execute=\\enrol_imsenterprise\\task\\cron_task

Windows servers do not require the extra escapes. Thus the command is simply

php schedule_task.php --execute=\enrol_imsenterprise\task\cron_task

Database transfer

A command line script for Database transfer may be found in admin/tool/dbtransfer/cli/migrate.php.

Purge caches

You can purge caches using this script:

 php admin/cli/purge_caches.php

Fix course / module sequences

In rare cases (such as after upgrading from a very old version of Moodle), the course / section / module sequence data can be out of sync. This can cause various problems for affected courses, such as sections not appearing, backups failing, pages not displaying etc. There is a specific check to check for errors caused by this problem, and to fix the data in the database if they are found. To run this script please use the command below:

 php admin/cli/fix_course_sequence.php -c=* --fix

This will check every course in Moodle and report which ones had errors and were fixed.

Fix orphaned question categories

When a quiz is created, a new question category for the quiz is automatically created. In versions of Moodle prior to 2.9.1, if the quiz is deleted, the question category and any questions in the category remain in database. These orphaned question categories may be fixed by running the admin/cli/fix_orphaned_question_categories.php script with the --fix option.

Search and replace text

This script can be used to search and replace text throughout the whole database. Use carefully and backup first always. More info in Search and replace tool.

php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost

Options: --search=STRING String to search for. --replace=STRING String to replace with. --shorten Shorten result if necessary. --non-interactive Perform the replacement without confirming. -h, --help Print out this help.

See also

  • MDL-35736 - Manage plugins via command line
  • MDL-36237 - Resort course list via CLI
  • MOOSH - MOOdle SHell. It is a commandline tool that will allow you to perform most common Moodle tasks.