1.新增入口文件ConsoleBootstrap.php
放在站点根目录下
<?php
$yiic='./framework/yiic.php';//引入框架
$config='./protected/config/console.php';//配置文件目录
defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($yiic);
2.新增配置文件console.php
/protected/config下新建配置文件console.php, 配置类似main.php
<?php
return array (
'basePath' => dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . '..',
'name' => 'My Console Application',
'import' => array (
'application.models.*',
'application.components.*',
'application.components.base.*',
),
'components' => array (
// Main DB connection
'db' => array (
'connectionString' => 'mysql:host=localhost;dbname=test',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'root',
'charset' => 'utf8'
),
...
)
);
3.新建计划任务TestCommand
<?php
class TestCommand extends CConsoleCommand
{
public function run($args)
{
//业务逻辑
}
}
4.手动执行(根目录下)
$ php ConsoleBootstrap test
5.服务器Crontab部署
$ crontab -e
*/30 * * * * cd /data/wwwroot/www.test.com && php ConsoleBootstrap.php test
网友评论