yii框架通過控制臺命令創(chuàng)建定時(shí)任務(wù)示例
假設(shè)Yii項(xiàng)目路徑為 /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中,可以使用項(xiàng)目的配置信息和Yii的各種方法
<?php
class TestCommand extends CConsoleCommand
{
public function run()
{
...
}
}
4. 創(chuàng)建定時(shí)任務(wù)
$ crontab -e
插入
1 * * * * /home/php/bin/php -f /home/apps/protected/commands/crons.php Test &
即為每小時(shí)的第一分鐘執(zhí)行TestCommand類中的內(nèi)容,類似的可以在/home/apps/protected/commands/下新建其他類,使用命令行執(zhí)行。
相關(guān)文章
php實(shí)現(xiàn)的任意進(jìn)制互轉(zhuǎn)類分享
這篇文章主要介紹了php實(shí)現(xiàn)的任意進(jìn)制互轉(zhuǎn)類分享,本文直接給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-07-07
php實(shí)現(xiàn)paypal 授權(quán)登錄
本文給大家分享的是php實(shí)現(xiàn)的paypal授權(quán)登錄的代碼,十分的簡單實(shí)用,有需要的小伙伴可以參考下。2015-05-05
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法詳解
本篇文章是對AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
PHP使用CURL獲取302跳轉(zhuǎn)后的地址實(shí)例
這篇文章主要介紹了PHP使用CURL獲取302跳轉(zhuǎn)后的地址實(shí)例,需要的朋友可以參考下2014-05-05
laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer)
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

