yii框架通過控制臺命令創(chuàng)建定時任務(wù)示例
假設(shè)Yii項目路徑為 /home/apps/
1. 創(chuàng)建文件 /home/apps/protected/commands/crons.php
<?php
$yii = '/home/apps/framework/yii.php';
require_once($yii);
$configFile = dirname(__FILE__).'/../config/console.php';
Yii::createConsoleApplication($configFile)->run();
2. 創(chuàng)建需要的配置文件 /home/apps/protected/config/console.php,配置需要的組件、數(shù)據(jù)庫連接,日志等信息,格式類似主配置文件main.php
<?php
return array(
'basePath'=>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的各種方法
<?php
class TestCommand extends CConsoleCommand
{
public function run()
{
...
}
}
4. 創(chuàng)建定時任務(wù)
$ crontab -e
插入
1 * * * * /home/php/bin/php -f /home/apps/protected/commands/crons.php Test &
即為每小時的第一分鐘執(zhí)行TestCommand類中的內(nèi)容,類似的可以在/home/apps/protected/commands/下新建其他類,使用命令行執(zhí)行。
相關(guān)文章
tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法
這篇文章主要介紹了tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法,結(jié)合實例形式分析了thinkPHP5框架多數(shù)據(jù)庫查詢的相關(guān)配置、初始化及調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-01-01PHP快速導(dǎo)出百萬級數(shù)據(jù)到CSV或者EXCEL文件
這篇文章主要介紹了PHP快速導(dǎo)出百萬級數(shù)據(jù)到CSV或者EXCEL文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11laravel 判斷查詢數(shù)據(jù)庫返回值的例子
今天小編就為大家分享一篇laravel 判斷查詢數(shù)據(jù)庫返回值的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP加密3DES報錯 Call to undefined function: mcrypt_module_open()
這篇文章主要介紹了PHP加密3DES報錯 Call to undefined function: mcrypt_module_open() 如何解決的相關(guān)資料,需要的朋友可以參考下2016-04-04ThinkPHP3.1.x修改成功與失敗跳轉(zhuǎn)頁面的方法
這篇文章主要介紹了ThinkPHP3.1.x修改成功與失敗跳轉(zhuǎn)頁面的方法,涉及thinkPHP底層源文件中相關(guān)跳轉(zhuǎn)代碼的設(shè)置與修改操作技巧,需要的朋友可以參考下2017-09-09