By Sebastiaan de Jonge, published on Wednesday, October 10, 2012 at 19:30

Commandline scripts can be useful to perform certain background tasks for your TYPO3 installation. In this short tutorial I will demonstrate how to create TYPO3 based CLI scripts for your own extensions.

Getting started

To get started, I'm going to begin with an empty dummy extension I created using the old-fashioned extension kickstarter. This extension is completely empty, it contains no modules, plugins or services of whatsoever.

Of course you are free to implement this in any way you wish.

Creating the CLI script

First of all we will create a folder inside the extension root called "cli" (which stands for command-line interface). Inside here we're going to place our main script which will be executed. I have called mine tx_sdjcli_time.php. It starts off with the following bit of code.

<?php
if (!defined('TYPO3_cliMode')) {
    die('You cannot run this script directly!');
}
?>

The condition used is to prevent the script from being executed anywhere other than officially via the TYPO3 cli_dispatch.phpsh script, found in the typo3 folder of your TYPO3 installation.

I have decided to keep it simple, my CLI script will simply echo the current time into the console. Completing my script, it looks like the following.

<?php
if (!defined('TYPO3_cliMode')) {
    die('You cannot run this script directly!');
}
echo time()."\n";
?>

Telling TYPO3 about our CLI script

We are not there yet, just creating a script inside the CLI folder isn't enough. We need to let TYPO3 know that there is a CLI script available, and define it's key. We do so by adding the following snippet to the extensions' ext_localconf.php file.

Note: if your extension doesn't contain an ext_localconf.php file, your can simply create one. It will automatically be loaded by the TYPO3 core.

<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['tx_sdjcli_time'] = array('EXT:sdj_cli/cli/tx_sdjcli_time.php', '_CLI_lowlevel');
?>

This configuration has 2 options, the first is of course the location of our CLI script. The second option is the backend user, used to execute the script. In my case I have chose to pick the _cli_lowlevel user. Since this user is normally already used for scheduler tasks and stuff like indexing. Of course you are free to change this to a specific user, if you wish to monitor certain operations specifically for your script.

Note: the user doesn't require any access rights and should have a random password. The password is never used so you will not need to write it down for later use.

Running the script

The script isn't run directly by executing the file but by using cli_dispatch.phpsh inside the typo3 folder of your installation. I have moved all the way up to the directory and executed the script like this.

./cli_dispatch.phpsh tx_sdjcli_time

In my case the following result pops up.

Passing parameters to your script

As an extra feature, I will demonstrate how you can pass and receive parameters to your script. In the example, I will make it possible to supply a time format to the script. If no format is passed on, the plain UNIX time will be printed. Let's have a look at the new script.

<?php
if (!defined('TYPO3_cliMode')) {
    die('You cannot run this script directly!');
}
$parameters = $_SERVER['argv'];
if(isset($parameters[1])) {
    echo date($parameters[1], time())."\n";
}
else {
    echo time()."\n";
}
?>

I have made a small modification, before anything is printed we will first check if the format parameter is set. I have used a very simple form of parameter checking, of course you may take this as far as you wish. In order to display the time in a human readable fashion, we can now execute the script as following.

./cli_dispatch.phpsh tx_sdjcli_time H:i

This will now result in the following output.

Note: If you are passing parameters with spaces that are actually grouped together, wrap them with quotes. E.g. "my parameter".

The extension

To make it easier for you to play around and explore, I have added the extension that I used for this post. Happy coding!

Comments

Your name
Your name - Friday, December 14, 2012 at 06:15

To make easy to understand to Creating CLI scripts for TYPO3. Excellent jobs. Happy coding.

Typo3 Customization Services in India

Steil Starter
Steil Starter - Thursday, June 13, 2013 at 11:21

Thanks for sharing the informative blog. Coding part of TYPO3 is very descriptive. Keep Posting.Good Going.

bhavana
bhavana - Tuesday, April 22, 2014 at 11:42

Hi,Thanks for providing information about CLI scripts for TYPO3. This will definately help to Typo3 developer to develop beautiful sites.