yii框架如何通过控制台命令创建定时任务

php中文网
发布: 2016-07-25 09:12:56
原创
1086人浏览过

假设Yii项目路径为 /home/apps/ 1,创建文件 /home/apps/protected/commands/crons.php

  1. $yii = '/home/apps/framework/yii.php';
  2. require_once($yii);
  3. $configFile = dirname(__FILE__).'/../config/console.php';
  4. Yii::createConsoleApplication($configFile)->run();
复制代码

2,创建需要的配置文件 /home/apps/protected/config/console.php,配置需要的组件、数据库连接,日志等信息,格式类似主配置文件main.php。

  1. return array(

  2. 'basepath'=>dirname(__file__).directory_separator.'..',
  3. 'name'=>'emergency',
  4. 'import'=>array(
  5. 'application.models.*',
  6. 'application.components.*',
  7. 'application.extensions.*',
  8. ),
  9. 'components'=>array(
  10. 'log'=>array(
  11. 'class'=>'clogrouter',
  12. 'routes'=>array(
  13. array(
  14. 'class'=>'cfilelogroute',
  15. 'levels'=>'info, warning, error',
  16. ),
  17. ),
  18. ),
  19. 'db'=>array(
  20. 'class'=>'application.extensions.phppdo.cpdodbconnection',
  21. 'pdoclass' => 'phppdo',
  22. 'connectionstring' => 'mysql:host=xxxx;dbname=xxx',
  23. 'emulateprepare' => true,
  24. 'username' => 'xxx',
  25. 'password' => 'xxx',
  26. 'charset' => 'utf8',
  27. 'tableprefix' => 'tbl_',
  28. ),
  29. ),
  30. 'params' => require('params.php'),

  31. );
复制代码

3,在 /home/apps/protected/commands/ 下新建 TestCommand 类,继承 CConsoleCommand,在TestCommand中,可以使用项目的配置信息和Yii的各种方法。

  1. class TestCommand extends CConsoleCommand
  2. {
  3. public function run()
  4. {
  5. ...
  6. }
  7. }
复制代码

4,创建定时任务 $ crontab -e 内容为: 1 * * * * /home/php/bin/php -f /home/apps/protected/commands/crons.php Test & 即为每小时的第一分钟执行TestCommand类中的内容,类似的可以在/home/apps/protected/commands/下新建其他类,使用命令行执行。

有关crontab的用法,可以参考:

Hugging Face
Hugging Face

Hugging Face AI开源社区

Hugging Face 135
查看详情 Hugging Face
  • crontab命令基础与实例
  • crontab命令的一些例子
  • linux安装crontab详解
  • crontab学习笔记
  • 学习linux设置定时任务的crontab命令
  • crontab 命令格式与例子
  • linux定时任务设置crontab学习
  • 不错的crontab教程


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号