1

I want to run yii console commands in shell. I run protected/yiic in the console, yet it fails, yielding -bash: yiic: command not found

What's the problem? See the shell commands and responses I have:

ls -l
...
-rw-r-----  1 srv50213 srv50213   94 Aug 21 09:26 test 
-rw-r-----  1 srv50213 srv50213   75 Aug  4 21:05 yiic 
-rw-r-----  1 srv50213 srv50213  395 Aug  4 21:05 yiic.bat
-rw-r-----  1 srv50213 srv50213  178 Aug  4 21:05 yiic.php
[srv50213@cl10-m protected]$ test
[srv50213@cl10-m protected]$ test
[srv50213@cl10-m protected]$ yiic
-bash: yiic: command not found
[srv50213@cl10-m protected]$ yiic.php
-bash: yiic.php: command not found
[srv50213@cl10-m protected]$ 

the yiic content:

#!/usr/bin/env php
<?php

require_once(dirname(__FILE__).'/yiic.php');

the yiic.php content:

defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));

defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once(dirname(__FILE__).'/yii.php');

if(isset($config))
{
  $app=Yii::createConsoleApplication($config);
  $app->commandRunner->addCommands(YII_PATH.'/cli/commands');
}
else
  $app=Yii::createConsoleApplication(array('basePath'=>dirname(__FILE__).'/cli'));

$env=@getenv('YII_CONSOLE_COMMANDS');
if(!empty($env))
  $app->commandRunner->addCommands($env);

$app->run();

Update

I've changes the permissions as suggested, yet now when i run ./yiic the result is : No such file or directory With ./test it works fine:

-rwxr-----  1 srv50213 srv50213   94 Aug 21 11:33 test 
-rwxr-xr-x  1 srv50213 srv50213   75 Aug  4 21:05 yiic 
-rw-r-----  1 srv50213 srv50213  178 Aug  4 21:05 yiic.php
[srv50213@cl10-m protected]$ test
[srv50213@cl10-m protected]$ ./test
test OK
[srv50213@cl10-m protected]$ ./yiic
: No such file or directory
[srv50213@cl10-m protected]$ echo $PATH  
  /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/web/users/srv50213/bin
[srv50213@cl10-m protected]$ 

What's wrong?

Update 2

Actually yiic.php contains:

<?php

// change the following paths if necessary
$yiic=dirname(__FILE__).'/../../framework/yiic.php'; 
$config=dirname(__FILE__).'/config/main.php';

require_once($yiic); 

where /../../framework/yiic.php is the path to the framework's yiic.php file exposed in main part of question. There is the yii.php file in the framework folder too.

[srv50213@cl10-m protected]$ cd ..
[srv50213@cl10-m doc]$ cd .. 
[srv50213@cl10-m htdocs]$ cd framework
[srv50213@cl10-m framework]$ ls -l
total 432
drwxr-s---  2 srv50213 srv50213   4096 Jul 16 23:37 base
drwxr-s---  3 srv50213 srv50213   4096 Jul 16 23:37 caching
drwxr-s---  5 srv50213 srv50213   4096 Jul 16 23:37 cli
drwxr-s---  2 srv50213 srv50213   4096 Jul 16 23:37 collections
drwxr-s---  2 srv50213 srv50213   4096 Jul 16 23:37 console
drwxr-s---  4 srv50213 srv50213   4096 Jul 16 23:38 db
drwxr-s---  9 srv50213 srv50213   4096 Jul 16 23:38 gii
drwxr-s---  4 srv50213 srv50213   4096 Jul 16 23:41 i18n
drwxr-s---  2 srv50213 srv50213   4096 Jul 16 23:41 logging

drwxr-s--- 11 srv50213 srv50213   4096 Jul 16 23:45 web
-rw-r-----  1 srv50213 srv50213  37100 Jul 16 23:37 YiiBase.php
-rw-r-----  1 srv50213 srv50213    358 Jul 16 23:37 yiic
-rw-r-----  1 srv50213 srv50213    577 Jul 16 23:37 yiic.bat
-rw-r-----  1 srv50213 srv50213    834 Aug 21 13:05 yiic.php
-rw-r-----  1 srv50213 srv50213 304900 Jul 16 23:37 yiilite.php
-rw-r-----  1 srv50213 srv50213    638 Jul 16 23:37 yii.php
-rw-r-----  1 srv50213 srv50213    175 Jul 16 23:37 yii-powered.png
-rw-r-----  1 srv50213 srv50213    694 Jul 16 23:37 yiit.php
drwxr-s---  4 srv50213 srv50213   4096 Jul 16 23:45 zii
[srv50213@cl10-m framework]$ 

Should i change mode (chmode) for yii.php and yiic.php in framework folder too?

Update 3

as suggested i ran [srv50213@cl10-m protected]$ php yiic.php and it issued php debug info:

    exception 'CException' with message 'Property "CConsoleApplication.defaultController" is not defined.' in /home/srv50213/htdocs/framework/base/CComponent.php:173
    Stack trace:
    #0 /home/srv50213/htdocs/framework/base/CModule.php(515): CComponent->__set('defaultControll...', 'site')
    #1 /home/srv50213/htdocs/framework/base/CApplication.php(161): CModule->configure(Array)
    #2 /home/srv50213/htdocs/framework/YiiBase.php(125): CApplication->__construct('/home/srv50213/...')
    #3 /home/srv50213/htdocs/framework/YiiBase.php(113): YiiBase::createApplication('CConsoleApplica...', '/home/srv50213/...')
    #4 /home/srv50213/htdocs/framework/yiic.php(23): YiiBase::createConsoleApplication('/home/srv50213/...')
    #5 /home/srv50213/htdocs/doc/protected/yiic.php(7): require_once('/home/srv50213/...')

DONE

Thank you to everyone. One problem was in there (as stated in debug info) /home/srv50213/htdocs/framework/yiic.php(23) with $config - $app=Yii::createConsoleApplication($config);. So I've changed the $config var in /home/srv50213/htdocs/doc/protected/yiic.php(5) from $config=dirname(__FILE__).'/config/main.php'; to $config=dirname(__FILE__).'/config/console.php'; and it has worked!!!

Should i still run php yiic.php instead of ./yiic for example in cron command?

  • You might want to add <?php to the beginning of yiic.php, but that's unlikely to cause the problem with : No such file or directory. That's more likely a space somewhere, such as the one at the beginning of your $PATH. – MattBianco Aug 21 '14 at 9:12
  • 1
    Try the command php yiic.php from the folder that contains yiic.php, yiic, and test. – MattBianco Aug 21 '14 at 9:23
  • Do you have two files with the same name yiic, in directories protected and in framework? Same for yiic.php? – Volker Siegel Aug 21 '14 at 9:29
  • @MattBianco, this command php yiic.php issued in real debug info - [srv50213@cl10-m protected]$ php yiic.php exception 'CException' with message 'Property "CConsoleApplication.defaultController" is not defined.' in /home/srv50213/htdocs/framework/base/CComponent.php:173 Stack trace: #0 /home/srv50213/htdocs/framework/base/CModule.php(515): CComponent->__set('defaultControll...', 'site') #1 /home/srv50213/htdocs/framework/base/CApplication.php(161): CModule->configure(Array) #2 /home/srv50213/htdocs/framework/YiiBase.php(125): – Igor Savinkin Aug 21 '14 at 9:29
  • 1
    @IgorSavinkin - please edit your Q with this stuff. Keep adding update sections as you go along, rather than dump this in a comment. – slm Aug 21 '14 at 9:31
2

There are two problems:

The first is that you do not have the execute permission:

Add the permission for you with:

$ chmod u+x yiic

It gives u, the user - you - the x, execute permission.


The second, separate issue is about how you call the program, and how it is found.
Now you have the execute permission, but

$ yiic

will probably still give you a command not found error.

That is because a command you run is searched for in the directories listed in the variable $PATH - which does not include the current directory normally (and should not include it for security reasons).

But you can give a file name of the command to run, by including a directory path for the command file. The simplest variant of this is just using the current directory:

$ ./yiic

That should finally work!

If it works without the ./ in front too, then you have the current directory, ., in your $PATH - take a look at it:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


(Oh, and then, there is the famous issue of using the file name test for testing something... that's a pretty bad trap I would say - you are not the first person getting bitten by this one... )

  • thank you, it really the thing you stated. But when i run ./yiic the responce is : No such file or directory. What's wrong? – Igor Savinkin Aug 21 '14 at 7:30
  • For ./yiic to work, you need to be in the same directrory. Otherwise, you need to use either a different relative path like foo/yiic if you are in your home directory and the file is in /home/igor/foo/yiic, or an absolute path, like /home/igor/foo/yiic. – Volker Siegel Aug 21 '14 at 7:35
  • but php is trully installed on server. [srv50213@cl10-m protected]$ command -v php /usr/bin/php how to resolve it? – Igor Savinkin Aug 21 '14 at 7:45
  • The #!/usr/bin/env php should find the php program in $PATH, and it's in $PATH. But if you want to test, you could try #!/usr/bin/php as first line instead. But the error message : No such file or directory is strange. I do not know php actually, so I can only guess, maybe it's the yiic.php that is not found inside yiic. Hmm... but you also require a file yii.php - are you sure it exists? I can not tell from the name, as I do not know what "yiic" or "yii" mean. – Volker Siegel Aug 21 '14 at 8:36
  • 1
    @IgorSavinkin I think yii.php may be missing indeed - the line require_once(dirname(__FILE__).'/yii.php'); reads it from the current directory, but the ls does not show it. But it shows ... I just noticed - so check whether is is there. – Volker Siegel Aug 21 '14 at 8:45
2

You should to remove Win (CRLF) in first line;

If you open file in mcedit you will see:

!/usr/bin/env php^M

change it to

!/usr/bin/env php

  • That's a good one ! – anno Nov 5 '16 at 17:46
1

The permissions on the file yiic are not set so that it can be executed.

-rw-r-----  1 srv50213 srv50213   75 Aug  4 21:05 yiic 

You can set it like so:

$ chmod u+x yiic

It should then look like this:

-rwxr-----  1 srv50213 srv50213   75 Aug  4 21:05 yiic 

Why did test work?

You may think that you were running test but in actuality you were running one of the nother commands called test which are located here:

$ type -a test
test is a shell builtin
test is /usr/bin/test
test is /bin/test

This is assuming that you're using Bash as your shell, but the cause is likely to be the same, that you're running another version of test.

To make sure you're running a command that's present in the current directory, it's always a good idea to prefix it with a dot slash, i.e.: ./test. This forces the shell to run test from the current location, rather than searching your $PATH looking for one to run.

  • @sim, thanks for the Why did test work? section. – Igor Savinkin Aug 21 '14 at 8:06
  • @IgorSavinkin - you're welcome. Thanks for the Q. – slm Aug 21 '14 at 8:08
  • I have #!/usr/bin/env php at the beginning of yiic file (taken from yii framework). So what does it mean? Should i uncomment it for running php.exe? – Igor Savinkin Aug 21 '14 at 8:11
  • @IgorSavinkin that's making that script a PHP script. That line is calling the PHP interpreter which will then parse the remainder of that file, executing its contents. I would seem there is more to that yiic file, than what's being shown here to us. – slm Aug 21 '14 at 8:14
  • seems i have no path to php.exe in $PATH: – Igor Savinkin Aug 21 '14 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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