Tuesday, January 25, 2011

How to run a cron job with Yii

Sometimes you need to run code on a timer, such as sending out email reminders based on database tables. This is how to do it.

1. In /protected/commands, add a file GoCommand.php with this content:
<?php

class GoCommand extends CConsoleCommand
{
    public function run($args)
    {
      echo 'Hello, world';
    }
}

?>
2. If you want to use your models, components and db connection, in /protected/config/console.php, copy over your 'import' and 'components' arrays from your main.config:

<?php

// This is the configuration for yiic console application.
return array(
 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
 'name'=>'My Console Application',

     // autoloading model and component classes
 'import'=>array(
  'application.models.*',
  'application.components.*',
 ),

     // application components
 'components'=>array(

  'db'=>array(
   'connectionString' => 'mysql:host=localhost;dbname=my_db',
   'emulatePrepare' => true,
   'username' => 'root',
   'password' => 'password',
   'charset' => 'utf8',
   'tablePrefix'=>'tbl_',
  ),


 ),
);
3. then using the command prompt (cmd.exe in Windows), change directories to the protected folder of your web app, and type
yiic Go
and it should run your command.

You can use your models and components and most parts of the Yii framework, but web application stuff won't work, like sessions.

4. In Windows, you can use Task Scheduler to run the PHP file using the command line. Here's an article on how to do it:

http://www.devx.com/DevX/Article/39900/1763/page/3

18 comments:

  1. Don't work for me :(

    ReplyDelete
  2. i am already generate a action in controller that generate xml file i want access that url from command prompt?ple tell as soon as possible...

    ReplyDelete
  3. how to execute url in your yii console?

    echo Yii::app()->createAbsoluteUrl('cron/run');

    ReplyDelete
  4. Thank you the post, that is what i am looking for. I will try it :)

    ReplyDelete
  5. that's sound useful for me, thank you

    ReplyDelete
  6. that's sound useful for me, thank you

    ReplyDelete
  7. Thank for share . i can it . thanks thanks :d

    ReplyDelete
  8. thanks you

    echo Yii::app()->createAbsoluteUrl('cron/run');

    ReplyDelete
  9. Thanks you the post :)
    very good !!!
    see you gain

    ReplyDelete
  10. It looks so simple, but it wont work for me. I did everything you said to do, and yiic cant see my command. I get this output:

    Yii command runner (based on Yii v1.1.12)
    Usage: C:\Websites\[site path]\yiiframework\yiic [parameters...]

    The following commands are available:
    - message
    - migrate
    - shell
    - webapp

    To see individual command help, use the following:
    C:\Websites\[site path]\yiiframework\yiic help

    I have been scouring google for hours and i cant find a solution! Anyone have any suggestions?

    ReplyDelete
  11. I figured out why yiic would not see my commands.....someone on my project had removed the yiic file, the yiic.php and the yiic.bat from the protected directory, so it was pulling those files from the framework directory instead - which was putting the base bath as the wrong path, and not loading the config file.

    make sure those 3 files are in your protected dir.

    i just wasted 3 hours figuring that out.

    ReplyDelete
  12. http://www.devx.com/DevX/Article/39900/1763/page/3 the webpage is not available...
    It shows Page Not found error

    ReplyDelete
  13. Yii command runner (based on Yii v1.1.12)
    Usage: C:\Websites\[site path]\yiiframework\yiic [parameters...]

    ReplyDelete
  14. thanks you

    echo Yii::app()->createAbsoluteUrl('cron/run');

    ReplyDelete
  15. Yii command runner (based on Yii v1.1.12)
    Usage: C:\Websites\[site path]\yiiframework\yiic [parameters...]

    ReplyDelete