yii linux 自动执行脚本

今天用yii开发的系统中要配置一个自定执行的脚本


1.配置好product/config/console.php里面需要用到的组件,像数据库连接


'db'=>array(
    'connectionString' => 'mysql:host=localhost;dbname=testdrive',
    'emulatePrepare' => true,
    'username' => 'root',
    'password' => '',
),
2.继承CConsoleCommand写入自己的命令类。


Yii提供了两种方式去执行,如果你执行单一的任务,直接在run方法里面写,另外一种就和Controller(控制器)中的Action(动作)类似,前面增加actionXXX,在framework/console下提供了很多实例供我们参考使用。例如CHelpCommand直接使用run:


class CHelpCommand extends CConsoleCommand
{
    /**
     * Execute the action.
     * @param array $args command line parameters specific for this command
     */
    public function run($args)
    {
        $runner=$this->getCommandRunner();
        $commands=$runner->commands;
        if(isset($args[0]))
            $name=strtolower($args[0]);
        if(!isset($args[0]) || !isset($commands[$name]))
        {
            if(!emptyempty($commands))
            {
                echo "Yii command runner (based on Yii v".Yii::getVersion().")\n";
                echo "Usage: ".$runner->getScriptName()." <command-name> [parameters...]\n";
                echo "\nThe following commands are available:\n";
                $commandNames=array_keys($commands);
                sort($commandNames);
                echo ' - '.implode("\n - ",$commandNames);
                echo "\n\nTo see individual command help, use the following:\n";
                echo "   ".$runner->getScriptName()." help <command-name>\n";
             }else{
                echo "No available commands.\n";
                echo "Please define them under the following directory:\n";
                echo "\t".Yii::app()->getCommandPath()."\n";
             }
         }else
            echo $runner->createCommand($name)->getHelp();
    }
    /**
     * Provides the command description.
     * @return string the command description.
     */
    public function getHelp()
    {
        return parent::getHelp().' [command-name]';
    }
}
接下来使用使用Action执行清理命令


class CleanupCommand extends CConsoleCommand {
 
    private $sessionTableName = 'it_common_session';
 
    /**
     * 自动执行清理Session,每2分钟.
     */
    public function actionSession() {
        $now = time();
        $sql="DELETE FROM {$this->sessionTableName} WHERE lastactivity < $now";
        $command = Yii::app()->db->createCommand($sql);
        $command->execute();
    }
 
    /**
     * 清理系统缓存,每天早上7:30
     */
    public function actionCache() {
        Yii::app()->cache -> flush();
        Yii::app()->dbcache -> flush();
        Yii::app()->fcache -> flush();
    }
 
    /**
     * 清除上传临时文件
     */
    public function actionTempfiles() {
        $dir = Yii::getPathOfAlias('site.frontend.www.uploads.temp').'/';
        foreach(glob($dir.'*.*') as $v){
            unlink($v);
        }
    }
}
3.使用Linux命令, vi crontab -e 打开自动执行tab, 增加一行


30 2 * * * php /path/to/console.php cleanup xxx这里的参数放action >> /path/to/logfile
需要在Yii应用目录下创建和入口文件同级的console.php:


<?php
    $yiic=dirname(__FILE__).'/protected/yiic.php';
    $config=dirname(__FILE__).'/protected/config/console.php';
    @putenv('YII_CONSOLE_COMMANDS='.dirname(__FILE__).'/protected/commands');
 
    require_once($yiic);
上面的意思是说每天晚上两点自动执行清理任务,简单几步就完成了强大的自动化功能任务,是不是够简单?


常见问题:


1).如果crontab 没有自动执行,仔细检测你的语法是否正确。


2).确定protected/yiic 文件是否有执行权限, 如果没有使用命令 chmod +x yiic 授权

转载于:https://my.oschina.net/surjur/blog/511781

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值