TYPO3 kennt seit Version 4.1 zwei verschiedene Wege, wie man einen Cronjob ausführen bzw. generell eine Aufgabe über das CLI (Command Line Interface) bzw. die Konsole starten kann. Ich werde hier der Vollständigkeit halber beide Wege aufzeigen und diese in “State-of-the-Art” und ”Old School” einteilen. Der Vorteil der neueren Methode ist, dass man sich um Pfade usw. nicht mehr kümmern muss, sondern alle Gemeinsamkeiten von TYPO3 bereitgestellt werden und somit die eigentliche Aufgabe, die Entwicklung des spezifischen Skripts, im Vordergrund bleibt.
State-of-the-Art
Für neu erstellte CLI-Skripte sollte stets dieser Weg eingeschlagen werden.
- Systemerweiterung “lowlevel” installieren (ist standardmäßig installiert)
- Erstellen eines BE-Benutzers beginnend mit “_cli_” (z.B. “_cli_cronjob” – Passwort spielt dabei keine Rolle)
- Erstellen eines Ordners namens “cli” im Ordner der Extension (z.B. in “typo3conf/ext/extensionkey/”)
- Erstellen einer Datei (z.B. “class.tx_extensionkey_cli.php” – Name grundsätzlich egal) im in Schritt 3 erstellten Ordner “cli” mit dem Inhalt [1]
- Erstellen (sofern noch nicht vorhanden) der Datei “ext_localconf.php” im in Schritt 3 erstellten Ordner “cli” im Ordner der Extension (z.B. in “typo3conf/ext/extensionkey/”) mit dem Inhalt [2] (vorhandenen Inhalt nicht überschreiben, sondern nur hinzufügen)
- Aufrufen des Skripts mit absolutem Pfad (z.B. “/var/www/typo3/cli_dispatch.phpsh extensionkey job”)
Hinweis: Sollte das Skript nicht ausgeführt werden, bitte überprüfen ob die Datei cli_dispatch.phpsh die erforderlichen Rechte (0755 bzw. rwxr-xr-x) aufweist.
[1]:
<?php
/***************************************************************
* Copyright notice
*
* (c) 2008 Your name <email@example.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
if (!defined ('TYPO3_cliMode')) die ('Access denied: CLI only.');
require_once(PATH_t3lib.'class.t3lib_cli.php');
class tx_extensionkey_cli extends t3lib_cli {
var $prefixId = 'tx_extensionkey_cli'; // Same as class name
var $scriptRelPath = 'cli/class.tx_extensionkey_cli.php'; // Path to this script relative to the extension dir.
var $extKey = 'extensionkey'; // The extension key.
function tx_extensionkey_cli() {
parent::t3lib_cli();
$this->cli_options = array_merge($this->cli_options, array(
));
$this->cli_help = array_merge($this->cli_help, array(
'name' => 'extensionkey CLI',
'synopsis' => 'synopsis',
'description' => 'description',
'examples' => 'typo3/cli_dispatch.phpsh ' . $this->extKey . ' TASK',
'author' => '(c) 2008 Your name <email@example.com>',
));
// read backend conf
$this->conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]);
}
function cli_main($argv) {
// disable output buffer
ob_end_clean();
// validate input
$this->cli_validateArgs();
// select called function
switch ((string)$this->cli_args['_DEFAULT'][1]) {
case 'job':
$this->job();
break;
default:
$this->cli_help();
break;
}
}
function job() {
$this->cli_echo("Press keyn");
$input = $this->cli_keyboardInput();
$this->cli_echo('Your input: ' . $input . "n");
}
}
$extensionkey = t3lib_div::makeInstance('tx_extensionkey_cli');
$extensionkey->cli_main($_SERVER['argv']);
?>
[2]:
<?php
## Setting up script that can be run through cli_dispatch.phpsh
if (TYPO3_MODE == 'BE') {
$TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'][$_EXTKEY] = array('EXT:'.$_EXTKEY.'/cli/class.tx_extensionkey_cli.php','_CLI_cronjob');
}
?>
Old School
Für neu erstellte CLI-Skripte sollte stets der “State-of-the-Art”-Weg eingeschlagen werden.
- Analog zu “State-of-the-Art”
- Analog zu ”State-of-the-Art”
- Analog zu ”State-of-the-Art”
- Erstellen der Datei “conf.php” im in Schritt 3 erstellten Ordner “cli” mit dem Inhalt [3]
- Erstellen der Datei “cronjob.phpsh” im in Schritt 3 erstellten Ordner “cli” mit dem Inhalt [4]
- Aufrufen des Skripts mit absolutem Pfad (z.B. “/var/www/typo3conf/ext/extensionkey/cli/cronjob.phpsh”)
Hinweis: Sollte das Skript nicht ausgeführt werden, bitte überprüfen ob die Datei cronjob.phpsh die erforderlichen Rechte (0755 bzw. rwxr-xr-x) aufweist.
[3]:
<?php
// DO NOT REMOVE OR CHANGE THESE 3 LINES:
define('TYPO3_MOD_PATH', '../typo3conf/ext/extensionkey/cli/');
$BACK_PATH = '../../../../typo3/';
$MCONF['name'] = '_CLI_cronjob'; //use name of your BE-User
?>
[4]:
#! /usr/bin/php -q
<?php
// *****************************************
// Standard initialization of a CLI module:
// *****************************************
// Defining circumstances for CLI mode:
define('TYPO3_cliMode', TRUE);
// Defining PATH_thisScript here: Must be the ABSOLUTE path of this script
in the right context:
// This will work as long as the script is called by it's absolute path!
define('PATH_thisScript',$_ENV['_']?$_ENV['_']:$_SERVER['_']);
// Include configuration file:
require(dirname(PATH_thisScript).'/conf.php');
// Include init file:
require(dirname(PATH_thisScript).'/'.$BACK_PATH.'init.php');
# YOUR CODE GOES HERE
?>
Die Diskussion zu “Old School” vs. “State-of-the-Art” der TYPO3 Dev-Mailinglist kann auf
Nabble nachgelesen werden.
Recent Comments