美文网首页
Yii1.1 利用CConsoleCommand 实现计划任务

Yii1.1 利用CConsoleCommand 实现计划任务

作者: 你与时光终会散 | 来源:发表于2018-12-06 15:58 被阅读0次
    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
    

    相关文章

      网友评论

          本文标题:Yii1.1 利用CConsoleCommand 实现计划任务

          本文链接:https://www.haomeiwen.com/subject/zpuocqtx.html