Command line scripts
Table of contents:
What is a CLI script
A command line script is a php script which can have parameters
Cronjob vs CLI scripts
Cronjob and CLI scripts are similar in the way that they are both run from the command line. However, there are two important differences between CLI scripts and cronjob scripts:
- CLI scripts can have parameters, while cronjob scripts cannot take any parameters
- Cronjob scripts set up the environment for you. That means creating a connection to the database, activating a siteaccess and activating extensions. If you implement a CLI script, you need to do all of this yourself.
Examples of CLI scripts are located in ./bin/php/*.php
Examples of Cronjob scripts are located in ./cronjobs/*.php
Example eZ script
This is a generic example cli, command line, shell script.
#!/usr/bin/env php <?php include_once( 'kernel/classes/ezscript.php' ); include_once( 'lib/ezutils/classes/ezcli.php' ); $cli =& eZCLI::instance(); /* general script settings array with the following keys: debug-message: false debug-output: false debug-include: false debug-levels: false debug-accumulator: false debug-timing: false use-session: false use-extensions: false use-modules: false user: false, or an array with the keys 'login' and 'password' description: 'eZ publish script', site-access: false min-version: false max-version: false */ $scriptSettings = array(); $scriptSettings['description'] = 'your description of this script comes here'; $scriptSettings['use-session'] = true; $scriptSettings['use-modules'] = true; $scriptSettings['use-extensions'] = true; $script =& eZScript::instance( $scriptSettings ); $script->startup(); /* script option configuration [optionname] value indications -------------------- : -> must have a value ; -> can have a value quantity indications ---------------------- ? -> min: 0; max: 1 * -> min: 0, max: unbounded + -> min: 1, max: unbounded */ $config = ''; /* script argument configuration */ $argumentConfig = ''; /* script option help specify a hash with option identifiers as keys and their help text as values */ $optionHelp = false; /* arguments */ $arguments = false; /* standard options array( 'debug' => true, 'colors' => true, 'log' => true, 'siteaccess' => true, 'verbose' => true, 'user' => false ) */ $useStandardOptions = true; $options = $script->getOptions( $config, $argumentConfig, $optionHelp, $arguments, $useStandardOptions ); $script->initialize(); /* $options['arguments'] is an array with all arguments passed to the script from the command line check for correct argument count: if ( count( $options['arguments'] ) != 1 ) { $script->shutdown( 1, 'wrong argument count' ); } show help: $script->showHelp(); auto-login as a specific user, without specifying a password: include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' ); $user = eZUser::fetchByName( 'admin' ); $userID = $user->attribute( 'contentobject_id' ); eZUser::setCurrentlyLoggedInUser( $user, $userID ); */ $script->shutdown( 0 ); ?>
Troubleshooting
Operations don't seem to work
Make sure you have enabled the loading of modules: $scriptSettings['use-modules'] = true;
Nodes are not fetched correctly
Make sure you have enabled the user command line options: $useStandardOptions = array( 'user' => true );
Now use the --login and --password command line options to log in with a privileged user.
The only output is ": No such file or directory"
Make sure your script has UNIX line endings (LF only).
Script ends with a fatal error
Fatal error: eZ publish did not finish its request The execution of eZ publish was abruptly ended, the debug output is present below.
You probably forgot to properly shutdown the script with $script->shutdown( 0 );
External resources
- Examples of simple, basic and advanced cli scripts.
- eZ publish Update Scripts
- Scripting in the old community documentation