How to pass parameter in cron command to Joomla CLI

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
webstudiolund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Sun Apr 03, 2011 12:00 pm

How to pass parameter in cron command to Joomla CLI

Post by webstudiolund » Wed Nov 04, 2015 12:30 pm

I can't get how to format parameters on a cron command line to make them available in a Joomla CLI script with the $this->input->get() function.

Example:
/usr/bin/php -f /home/domain/public_html/administrator/components/path/test.php xxxxxx ##
this makes the parameters xxxxxx and ## available in the argv array to any other php script, but it does not seem to work in a Joomla CLI appliction.

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4026
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: How to pass parameter in cron command to Joomla CLI

Post by itoctopus » Wed Nov 04, 2015 12:50 pm

Have you tried using wget and then passing the parameters as URL parameters (instead of script parameters)? For example

Code: Select all

wget 'http://[your-domain]/administrator/components/path/test.php?first_param=1&second_param=2'
(Note that the URL is enclosed in quotes).
Last edited by itoctopus on Wed Nov 04, 2015 1:47 pm, edited 1 time in total.
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 11031
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK

Re: How to pass parameter in cron command to Joomla CLI

Post by toivo » Wed Nov 04, 2015 1:30 pm

$argv is available when running from the command line in Joomla or other PHP scripts. The JInput class is available in the application environment, in other words in the browser mode. The script can check in which mode it is run, for example:

Code: Select all

<?php
$crlf		= "\r\n";
$version	= 'CLI';
if (function_exists('getallheaders') AND !function_exists('cli_get_process_title'))
{
	// run from browser
	$version	= 'browser';
	$crlf		= '<br />';
}
echo $crlf . 'PHP version = ' . phpversion() . ' in ' . $version . $crlf;
Toivo Talikka, Global Moderator
troubleshooting smtp and other articles http://talikka.com/joomla

webstudiolund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Sun Apr 03, 2011 12:00 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by webstudiolund » Wed Nov 04, 2015 5:56 pm

itoctopus wrote:Have you tried using wget and then passing the parameters as URL parameters (instead of script parameters)? For example

Code: Select all

wget 'http://[your-domain]/administrator/components/path/test.php?first_param=1&second_param=2'
(Note that the URL is enclosed in quotes).
nope, sorry, produces:

Code: Select all

root@vps [~]# /usr/bin/php -f wget 'https://domain/administrator/components/path/file.php?first_param=xxxxxx&second_param=#'
No input file specified.

Code: Select all

root@vps [~]# /usr/bin/php wget 'https://domain/administrator/components/path/file.php?first_param=xxxxxx&second_param=#'
Status: 404 Not Found
X-Powered-By: PHP/5.4.45
Content-type: text/html; charset=utf-8

No input file specified.

webstudiolund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Sun Apr 03, 2011 12:00 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by webstudiolund » Wed Nov 04, 2015 6:05 pm

toivo wrote:$argv is available when running from the command line in Joomla or other PHP scripts. The JInput class is available in the application environment, in other words in the browser mode. The script can check in which mode it is run, for example:

Code: Select all

<?php
$crlf		= "\r\n";
$version	= 'CLI';
if (function_exists('getallheaders') AND !function_exists('cli_get_process_title'))
{
	// run from browser
	$version	= 'browser';
	$crlf		= '<br />';
}
echo $crlf . 'PHP version = ' . phpversion() . ' in ' . $version . $crlf;
So if I get the answer

Code: Select all

# /usr/bin/php -f /home/domain/public_html/administrator/components/path/mode-test.php
<br />PHP version = 5.4.45 in browser<br />
, does that mean that there is no CLI version running?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 11031
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK

Re: How to pass parameter in cron command to Joomla CLI

Post by toivo » Wed Nov 04, 2015 6:45 pm

Oops, the test produces wrong results! It seems to find getallheaders(), which may be available in CLI, but in PHP 5.4 my logic failed because the function cli_get_process_title() became available only in 5.5.0, according to the manual: http://php.net/manual/en/function.cli-g ... -title.php

Even if you can boot Joomla functions in CLI mode and use the classes, the $_GET and $_POST arrays are dependent on HTTP requests, and CLI parameters are not HTTP.
Toivo Talikka, Global Moderator
troubleshooting smtp and other articles http://talikka.com/joomla

mbabker
Joomla! Hero
Joomla! Hero
Posts: 2179
Joined: Sun Feb 28, 2010 8:26 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by mbabker » Wed Nov 04, 2015 8:09 pm

*facepalm*

If you are writing a command line script that uses a JApplicationCli class (for example https://github.com/joomla/joomla-cms/bl ... ndexer.php) then you can use the JInput API the same way you would in a web request. The CLI application class by default populates $app->input with a JInputCli object, a subclass of JInput specialized for reading the switch parameters you pass into a CLI script. Assuming you called `php cli/finder_indexer.php --purge`, you could check if purge was set or had a value via $app->input->get('purge').

As pointed out, there are limitations. Data that is set via a web request obviously won't be present.
So long and thanks for all the fish.

Manually updating Joomla? See https://gist.github.com/mbabker/d7bfb4e ... 3607f89281

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4026
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: How to pass parameter in cron command to Joomla CLI

Post by itoctopus » Wed Nov 04, 2015 8:48 pm

wget is a standalone function. You run it as:

Code: Select all

wget 'https://domain/administrator/components/path/file.php?first_param=xxxxxx&second_param=#'
and not as:

Code: Select all

/usr/bin/php wget 'https://domain/administrator/components/path/file.php?first_param=xxxxxx&second_param=#'
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

webstudiolund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Sun Apr 03, 2011 12:00 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by webstudiolund » Sat Nov 07, 2015 8:50 am

itoctopus wrote:wget is a standalone function. You run it as:
OK, I suspected that. But I have to run this as a cron command line and pass the arguments to the Joomla CLI. In the instructions for the component the arguments should be prefixed with "--" and that does not work in my cron implementation. The "-" can only be used for the built-in cron options, and cron failes complaining on "Error in argument 3, char 2: no argument for option -". Without "--" both arguments I have to pass are passed successfully, which I have confirmed with a small test script that writes the contents of the argv array to a file.

Sadly the cli.php in /libraries/joomla/input/ does not handle arguments like name=value without preceding "-" or "--". I think it ought to be possible with a few lines like

Code: Select all

$eqPos = strpos($arg, '=');
			if ($eqPos === false)
				{
					$key = substr($arg, 0, $eqPos);
					$value = substr($arg, $eqPos + 1);
					$out[$key] = $value;
				}
in the protected function parseArguments(). Somehow I can't make it work.

Just noticed an error in my code. I need to negate the $eqPos condition.

mbabker
Joomla! Hero
Joomla! Hero
Posts: 2179
Joined: Sun Feb 28, 2010 8:26 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by mbabker » Sun Nov 08, 2015 6:29 pm

wget as it's written in those examples issues an HTTP web request. It isn't executing a file via the command line. You're mixing two concepts together massively right now.

If you are trying to execute a PHP file via a command line request, issue a "php /path/to/file.php --option1" style command in your cron manager. That file, similar to https://github.com/joomla/joomla-cms/bl ... ndexer.php should bootstrap the environment and execute a JApplicationCli class.

If you are trying to execute a web request from your cron command via wget, issue a "wget URL" command and the result will be processed as any other HTTP web request would be handled.
So long and thanks for all the fish.

Manually updating Joomla? See https://gist.github.com/mbabker/d7bfb4e ... 3607f89281

webstudiolund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Sun Apr 03, 2011 12:00 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by webstudiolund » Sun Nov 08, 2015 6:54 pm

mbabker wrote: If you are trying to execute a PHP file via a command line request, issue a "php /path/to/file.php --option1" style command in your cron manager.
Dear mbabker. My problem is that the cron implementation on a server I'm administrating does not accept the double dashes (--) as argument for the call to the file! As soon as it finds a "-" it tries to fit it to a built-in command, like -f and issues this error: "Error in argument 3, char 2: no argument for option -", or am I misinterpreting this message? Arguments not prefixed with -- are passed just fine; but then, they are not handled by the cli.php in /libraries/joomla/input/. If I just could add this in the function parseArguments() the problem should be solved.

mbabker
Joomla! Hero
Joomla! Hero
Posts: 2179
Joined: Sun Feb 28, 2010 8:26 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by mbabker » Sun Nov 08, 2015 8:08 pm

Unfortunately there isn't much to be done about that then if your system's cron processor is intercepting the options and processing them differently. Your other option is to not use Joomla's inbuilt CLI and just read the data you need direct from the superglobals. You'd just need to ensure you are adequately filtering the data (which JInput automatically does) if you go that route.
So long and thanks for all the fish.

Manually updating Joomla? See https://gist.github.com/mbabker/d7bfb4e ... 3607f89281

RedEye
Joomla! Ace
Joomla! Ace
Posts: 1460
Joined: Sat Jan 21, 2006 8:42 pm

Re: How to pass parameter in cron command to Joomla CLI

Post by RedEye » Tue Nov 17, 2015 12:09 am

webstudiolund wrote: Dear mbabker. My problem is that the cron implementation on a server I'm administrating does not accept the double dashes (--) as argument for the call to the file! As soon as it finds a "-" it tries to fit it to a built-in command, like -f and issues this error: "Error in argument 3, char 2: no argument for option -", or am I misinterpreting this message? Arguments not prefixed with -- are passed just fine; but then, they are not handled by the cli.php in /libraries/joomla/input/. If I just could add this in the function parseArguments() the problem should be solved.
CliInput class does not handle plain args, it just puts them to the public array args but not into the data array...

The error message you get, is probably because you're using the command wrong when using -f option. If you're using -f your first argument must be -- otherwise php thinks you are passing an option and not an argument.

Code: Select all

php -f file.php --var=value      <-- doesn't work, interpreted as option
php -f file.php -var=value       <-- doesn't work, interpreted as option
php -f file.php -- --var=value   <-- works, interpreted as argument
php -f file.php -- -var=value    <-- works, interpreted as argument
php file.php --var=value         <-- works, interpreted as argument
php file.php -var=value          <-- works, interpreted as argument


Locked

Return to “Joomla! 3.x Coding”