Heim > Backend-Entwicklung > PHP-Tutorial > yii框架如何通过控制台命令创建定时任务

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-07-25 09:12:56
Original
997 Leute haben es durchsucht

假设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的用法,可以参考:

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


Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage