您好,欢迎访问一九零五行业门户网

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

假设yii项目路径为 /home/apps/1,创建文件 /home/apps/protected/commands/crons.php
run();
复制代码
2,创建需要的配置文件 /home/apps/protected/config/console.php,配置需要的组件、数据库连接,日志等信息,格式类似主配置文件main.php。
dirname(__file__).directory_separator.'..', 'name'=>'emergency', 'import'=>array( 'application.models.*', 'application.components.*', 'application.extensions.*', ), 'components'=>array( 'log'=>array( 'class'=>'clogrouter', 'routes'=>array( array( 'class'=>'cfilelogroute', 'levels'=>'info, warning, error', ), ), ), 'db'=>array( 'class'=>'application.extensions.phppdo.cpdodbconnection', 'pdoclass' => 'phppdo', 'connectionstring' => 'mysql:host=xxxx;dbname=xxx', 'emulateprepare' => true, 'username' => 'xxx', 'password' => 'xxx', 'charset' => 'utf8', 'tableprefix' => 'tbl_', ), ), 'params' => require('params.php'),
);
复制代码
3,在 /home/apps/protected/commands/ 下新建 testcommand 类,继承 cconsolecommand,在testcommand中,可以使用项目的配置信息和yii的各种方法。
复制代码
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教程
其它类似信息

推荐信息