Run console app in background

Supposing I’m running a Yii application. I want my Yii app to call for a Yiic script which run in background so that my Yii app can continue to work without waiting the response of Yiic.

What I did :


					

$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';

    $runner = new CConsoleCommandRunner();

    $runner->addCommands($commandPath);

    $commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';

    $runner->addCommands($commandPath);

    $args = array('yiic', 'mail', 'a'=>'b');

    $runner->run($args);

From the tutorial, they didn’t teach how to do it.

Depending on your needs you might find this extension useful too: http://www.yiiframework.com/extension/tconsolerunner

I think that the extension runactions is better :)

But still I want to run yiic in background with pure yii code without the need of installing more ext

Edit, runactions doesn’t fullfill my need because it lauches through a http request, could cause a time out

Hi again,

I don’t really see the problem. Why don’t you just create a CConsoleCommand and use TConsoleRunner to start it from within your app? You could at least look at the source code of the extension to see what it does. It is not that complex as far as I remember. Then you may not even have to use the extension but only use the code parts you need.

cheers,

Hannes

Could you post an example or tell where I did wrong, I can’t seem to make it work.

MailCommand.php




class MailCommand extends CConsoleCommand

{

	public function actionSend()

	{

		//do mail stuffs and update, could take 1 min long

	}

}



In controller:




$console=new TConsoleRunner('console.php');

$console->run('mail send');



Edit: it worked :)

Though I need to add "&>" before /dev/null in the extension like suggested in some site

I did some debug and find that you use wrong arguments list

this must work:




    $commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';

    $runner = new CConsoleCommandRunner();

    $runner->addCommands($commandPath);

    args = array('scriptName.php', 'commandName', 'actionName', '--parameter=b');

    $runner->run($args);



Correct me if I’m wrong but I believe using CConsoleCommandRunner still runs the command through your web server, and not as a cli command.

Also, what’s up with all the necro postings lol. A lot of replies nowadays seem to be for threads from years back