I have a problem figuring out how make my command work, because I only have access via ftp.
The action is pretty simple:
public function actionRun($action = "default") {
$this->xml = simplexml_load_file('db.xml');
return $this->{$action}(); // executes the default() method
}
All I need is to somehow execute the php index.php mycommand run, but I'm lost as to how. This command should only be run once in the life of the whole app.
My question is, is it possible to run such a command? Maybe somehow invoke it through php?
Yii command line command are designed to run through Yii.
$ cd protected
$ ./yiic --help
$ ./yiic mycommand
If you only have ftp access, you may be out of luck, and will have to use some workaround, for example running a cron job, or creating a web page that invokes the last command in the code sample I provided.
echo exec('/my_yii_dir/protected/yiic mycommand');
Related
I'm working on a custom CLI command & I was wondering what's the best way to call other commands from the PHP code (without shell_exec() or similar).
For example:
When running "php bin/magento my:custom:command", it'll do it's thing & in the end will run "php bin/magento cache:flush".
Any Ideas?
Thanks.
The Magento CLI is built on top of Symfony Console. You can load up and run other commands with this component as such:
$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, $output);
$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);
More information here. Although it's unlikely to be a problem for you, please note that the documentation suggests this is not always the best idea:
Most of the times, calling a command from code that is not executed on the command line is not a good idea. The main reason is that the command's output is optimized for the console and not to be passed to other commands.
I have a directory that when accessed through the terminal, I run the command 'make' then 'make install' which subsequently builds a dictionary file. I want to automate this process, which will kick off when the user selects a button on the interface.
Using PHP in my web app I want to navigate to the directory which I have done so here:
chdir('../DictionaryFolder');
Then, I thought this PHP command would run the make and make install:
exec(make);
exec(make install);
But this does nothing.
Any help will be much appreciated!
You need to write the command like the following,
<?php
$output = shell_exec('make;make install;');
echo "<pre>$output</pre>";
?>
Shell exec will do the trick by calling $output via pre tag.
Exec() try to execute PHP like eval() in JS, shel_exec execute commande like you with CLI
I am building Symfony Application that use console commands. The same console command can be executed from controller thrue events, but it also can be run from terminal. How can I figure it out from where the command was run so that I can implement the user authentication if the command was run from terminal. If the command was run from controller then user already has permission to run. But if it has been run from terminal he must authenticate by username and password so that I check if he has necessary role?
You can check if your command was run from the console or from a controller using php_sapi_name() function or PHP_SAPI constant (which is similar to php_sapi_name())
if ('cli' === PHP_SAPI) {
// command was run from the console
} else {
// command was run from a controller
}
The symfony Console Application has no firewall layer like the HttpKernel.
The console component was built for small 'admin like' tasks, not sth that is facing an individual user.
The whole problem could be solved by implementing a commandbus pattern.
The "command" is created by the web controller that is secured and has a user and in the CLI command (for crontab) without any security checks.
Then its passed to the command bus that handles over to the Command Handler.
The Handler contains the current logic of the execute method. The Command Object would contain any data to execute these logic == your current input arguments and options.
The Symfony Command afterwards is very slim, like just passing input args to command handler. Like it should be.
If it is a long running task it could even be offloaded from the web request to an worker queue.
Please mind the naming collision. A symfony console command !== command bus command here.
For example, you could use SimpleBus inside Symfony:
https://github.com/SimpleBus
Also the recent blog post on the naming issue by #skoop:
http://leftontheweb.com/blog/2016/06/18/Command-or-Controller/
I have a PHP script that is run via CLI. In turn I want that script to call a bash script, but preferably I would like to break up the BASH requests so I can see the action as it is happening.
I can seem to set Environmental variables and they seem to exist between shell_exec() functions. But even when I have a source file like:
source ./bashes/functions.sh
And in the source file I use "export -f function-name" to export the functions in the script before executing the next line, the next line does not see the functions.
$file = file('./bashes/bash_testing.sh',FILE_SKIP_EMPTY_LINES);
//we want to watch this in realtime so write each line seperately to shell.
foreach($file as $command) {
$output->writeln(shell_exec($command));
}
The function $output->writeln is a helper function just to echo the returned result. But basically the error I get is
sh: now: command not found
now is defined as a function in the included bash_testing.sh shell script.
Anyone know how I can resolve this issue?
Here is the source to the ./bashes/functions.sh file:
function now {
date -u +%T
}
export -fx now
There is a way to maintain a single bash shell, execute commands and handle the return. I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
I would suggest not triggering a bash script but rather trigger the induvidual commands. That way you can handle the return and not have to build exception handling in bash.
After downloading you would simply use the following code:
//get a real bash shell.
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
$return1 = $shell->exeCmd($command1);
//logic to handle the return
$return2 = $shell->exeCmd($command2);
//logic to handle the return
..... etc
I am trying to pass parameters to command line and get the values using getopt() function. It works fine when I use
php file.php -a arg
But it does not show any value when I use
yiic shell file.php -a arg
I think I am using the incorrect syntax but I didn't get any proper result when I googled for the correct answer.
Any help will be appreciated.
Thanks in advance!
You should use the name of your command instead of the file name. For example, if you have a MyCommandNameCommand class that extends from CConsoleCommand you have to execute the following command from the command line:
yiic shell mycommandname arg1 arg2 ...
Hope it helps.
Well you just have to create a Yii command.
Check this out:
http://www.yiiframework.com/doc/guide/1.1/en/topics.console
In order to create your own command with custom parameters, it should be quite easy, following this SitemapCommand example:
http://www.yiiframework.com/doc/guide/1.1/en/topics.console#console-command-action
Why are you trying to use parameters in yiic shell command? This is a special command that as far as I can see it takes only one parameter. You can see the core file at framework\cli\commands\ShellCommand.php, but again I do not understand why are you messing with this command.
>> yiic help shell
USAGE
yiic shell [entry-script | config-file]
DESCRIPTION
This command allows you to interact with a Web application
on the command line. It also provides tools to automatically
generate new controllers, views and data models.
It is recommended that you execute this command under
the directory that contains the entry script file of
the Web application.
PARAMETERS
* entry-script | config-file: optional, the path to
the entry script file or the configuration file for
the Web application. If not given, it is assumed to be
the 'index.php' file under the current directory.