這篇文章主要介紹了Yii框架創建cronjob定時任務的方法,結合具體實例形式分析了Yii定時任務相關配置、實現步驟與注意事項,需要的朋友可以參考下
本文實例講述了Yii框架創建cronjob定時任務的方法。分享給大家參考,如下:
1. 新增環境配置
#protected/config/console.php
<?php require_once('env.php'); // This is the configuration for yiic console application. // Any writable CConsoleApplication properties can be configured here. return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'CMS Console', // application components 'components'=>array( //Main DB connection 'db'=>array( 'connectionString'=>DB_CONNECTION, 'username'=>DB_USER, 'password'=>DB_PWD, 'enableParamLogging'=>true, ), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), ), ), ), );
### 2. 新增定時任務執行模組#########protected/commands/crons.php###
<?php defined('YII_DEBUG') or define('YII_DEBUG',true); // including Yii require_once('/../framework/yii.php'); // we'll use a separate config file $configFile='/config/console.php'; // creating and running console application Yii::createConsoleApplication($configFile)->run();
class TestCommand extends CConsoleCommand { public function run($args) { //todo } }
30 0 * * * www php /path/to/crons.php Test >>/path/to/logs/test.log
30 0 * * * www php /path/to/crons.php Test param1 param2 ...
以上是在Yii中創建cronjob定時任務的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!