Yii 利用CConsoleCommand 实现计划任务
1、/protected/commands 下新建文件crons.php
4、linux 编辑crontab
5、注意事项
<?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文件里查找
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: PHP CURL POST 多个文件
