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

... 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.

  • Just to clarify, you are triggering the file through a cron? In the CLI projects I have done, I have always had everything contained in one file. So replacing the require gng_shared.php with the class itself. – YellowWebMonkey Nov 12 at 13:11
  • I added some clarification saying that in development I run the code from a terminal, but in production it will be triggered by a cron job. Unfortunately I had the same problem when I copied also the class defintion into the gng_rules.php file. – wanderlusted Nov 12 at 15:31

Normal echo commands don't work within in CLI commands. Try this instead: $this->out("started...");

  • in the "gng_rules.php" file I'm not in an object context, so I cannot use $this. In this file echo seems to work normally: it prints out "started", but it does not print out "executed" this is why I think that somehow the instantiation of the JApplicationCli object goes wrong... I tried using $this->out in the class defined in "gng_shared.php" but this does not change anything as I believe that this code is not executed at all due to the instantiaton problem. Anyway thanks for the hint! – wanderlusted Nov 14 at 7:53
  • As a disclaimer, I am not an expert on this stuff at all, I just know what I did in the few I have working. If there is any error at all, it will just exit without a message. So in the updated example, either the out or the echo is wrong, so it would exit out. For the execute, I use by itself instead of defining as variable, so just JApplicationCli::getInstance('GarbageCron')->execute(); – YellowWebMonkey Nov 14 at 11:40
up vote 0 down vote accepted

It turned out that the code above is good. However I ran it again the wrong php installation that caused the constructor of the parent object of JApplicationCli to silently exit. Running the code against the right php executable, it all works fine! Thanks for the hints!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.