Yii 利用CConsoleCommand 实现计划任务

本文介绍了如何在Yii框架中利用CConsoleCommand创建并配置计划任务。首先,在/protected/config下创建console.php配置文件,接着在/protected/commands目录下创建TestCommand.php,遵循特定命名规则。为了执行计划任务,需赋予yiic可执行权限,并确保/home/log目录具有写入权限。当任务出错时,可将错误信息重定向至/home/log日志文件以便排查问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、/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';
$configFile=dirname(dirname(__FILE__)).'/config/console.php';
// creating and running console application
?>


2、/protected/config下新建配置文件console.php, 配置类似main.php

<?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' => 'My Console Application',
        'import' => array (
                'application.models.*',
                'application.components.*',
                'application.components.base.*',
                'application.components.imgthumb.*',
                'application.models.form.*'
        ),
        'components' => array (
                // Main DB connection
                'db' => array (
                        'connectionString' => 'mysql:host=127.0.0.1;dbname=test',
                        'emulatePrepare' => true,
                        'username' => 'root',
                        'password' => 'root',
                        'charset' => 'utf8'                     
                ),
                'log' => array (
                        'class' => 'CLogRouter',
                        'routes' => array (
                                array (
                                        'class' => 'CFileLogRoute',
                                        'levels' => 'error, warning'
                                ) 
                        ) 
                ) 
        ) 
);


3、/protected/commands 下新建文件TestCommand.php,注意文件命名XyzCommand

<?php
/**
 * 自动化执行 命令行模式
 */
class TestCommand extends CConsoleCommand
{
    public function run($args)
    {
      $data = array('username' => 'ezreal', 'password' => date("Y-m-d H:i:s"));
        try {
            return Yii::app()->db->createCommand()->insert('gzc_user', $data);
        } catch (Exception $ex) {
            return null;
        }
    }

    public function test()
    {
        echo 'this is a test';
    }

}

4、linux 编辑crontab

*/2 * * * * php /var/www/html/trunk/gzcloud/protected/commands/crons.php test #插入数据操作
*/2 * * * * php /var/www/html/trunk/gzcloud/protected/commands/crons.php test test >> /home/log #在文件log中插入字符串



5、注意事项

①yiic 要给执行权限

②/home/log 要给写的权限

③第一个计划任务如果有错误 加上 >> /home/log  把错误信息写到log文件里,然后在log文件里查找


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值