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!!!
?>
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!!!
?>
What about working with MS sql connection and windows based development . Will this work then ?
ReplyDeleteThank you! You trully helped me a lot!
ReplyDeleteHi , 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.
ReplyDeletePlease help.
thanks for helping . But just need to modify query to run :
ReplyDelete*/10 * * * * php /var/www/tvmatches.com/public_html/tvshows/dev/protected/yiic go
good this is working fine
ReplyDeletephp protected/yiic cronjob
"Create cron.php file under project folder and write following:"
ReplyDeleteHere "project" means under "public_html" directory?
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.
ReplyDeletegreat
ReplyDeleteNice Blog , This is what I exactly Looking for , Keep sharing more blog
ReplyDeletePHP Yii framework development in India