Thursday 8 August 2013

How to create cron job in YII framework?

Hi All,

Create cron.php file under project folder and write following:

<?php
// change the following paths if necessary
$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/console.php';

require_once($yii);
Yii::createConsoleApplication($config)->run();

?>


Create console.php file under protected/config folder and write same data as written in main.php. see below:

/ This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Console Application',

    // preloading 'log' component
    'preload'=>array('log'),
        'import'=>array(
                'application.components.*',
                'application.models.*',
        ),

    // application components
    'components'=>array(
           
        // uncomment the following to use a MySQL database       
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=bollywood_news',
            'emulatePrepare' => true,
            'username' => $dbUsername,
            'password' => $dbPassword,
            'charset' => 'utf8',
                        'enableProfiling' => true,
        ),
       
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                                        'logFile'=>'cron.log',
                    'levels'=>'error, warning',
                ),
                                array(
                                        'class'=>'CFileLogRoute',
                                        'logFile'=>'cron_trace.log',
                                        'levels'=>'trace',
                                ),
            ),
                       
        ),
    ),
   
);

Now,

Create CronjobCommand.php file under protected/commands folder. Here cronjob is a command.

<?php

class CronjobCommand extends CConsoleCommand {

    public function run($args) {
        // your code will come here...
    }

}

Now,

You have two ways to run your command:

Go to your project folder and write following:

a. php cron.php cronjob
b. php protected/yiic cronjob


Cheers!!!





?>

9 comments:

  1. What about working with MS sql connection and windows based development . Will this work then ?

    ReplyDelete
  2. Thank you! You trully helped me a lot!

    ReplyDelete
  3. Hi , I need urgent help . I am running following command for cron job in linux.. */2 * * * * /var/www/tvmatches.com/public_html/tvshows/dev/protected/yiic Go where Go is GoCommand.php file extending CConsoleCommand but not getting mail after 2 minutes.

    Please help.

    ReplyDelete
  4. thanks for helping . But just need to modify query to run :

    */10 * * * * php /var/www/tvmatches.com/public_html/tvshows/dev/protected/yiic go

    ReplyDelete
  5. good this is working fine
    php protected/yiic cronjob

    ReplyDelete
  6. "Create cron.php file under project folder and write following:"

    Here "project" means under "public_html" directory?

    ReplyDelete
  7. Nice blog and nice post.I like the way you explained.Iam much more impressed.The person who created this post is a genius and knows how to keep the readers connected.If you want to know more about yii2 frameworks,have a look on yiioverflow.

    ReplyDelete
  8. Nice Blog , This is what I exactly Looking for , Keep sharing more blog
    PHP Yii framework development in India

    ReplyDelete