I have a Joomla website and I'm working on a Joomla command line interface application that would do some scheduled maintenance tasks. I follow the below article in building the cli app:
https://docs.joomla.org/How_to_create_a_stand-alone_application_using_the_Joomla!_Platform Wayback link to 2019 version of documentation
... and also took a look at the files in my "cli" directory, for example garbabecron.php to learn how this should work.
Please see below my test file that is basically identical to garbagecron.php:
<?php
/**
* @package Joomla.Cli
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
/**
* A command line cron job to trash expired cache data.
*/
// Initialize Joomla framework
const _JEXEC = 1;
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
/**
* Cron job to trash expired cache data.
*
* @since 2.5
*/
class GarbageCron extends JApplicationCli
{
/**
* Entry point for the script
*
* @return void
*
* @since 2.5
*/
public function doExecute()
{
$this->out("workin... \n");
echo "still working... \n";
}
}
echo "started... \n";
$cli = JApplicationCli::getInstance('GarbageCron')->execute();
echo "ready... \n";
During development I run the test.php
file from a terminal (in production it will be triggered by a cron job). It seems that the code runs until the line where I try to create and execute the JApplicationCli class, but then nothing happens. The program prints the trace "started", but then nothing else. It seems like the object instantiation fails, but I don't get any error message either in the terminal. Please see the terminal output below:
gonative@uk20 [~]# /usr/bin/php /home/gonative/public_html/dev/cli/test.php
Content-type: text/html; charset=UTF-8
started...
gonative@uk20 [~]#
What am I doing wrong? The tutorial suggest a slightly different syntax as the one used in the garbagecron.php file. Which one is the right one? Does the garbagecron.php works at all? I was unable to prove this.
Thanks, W.