|
假設Yii項目路徑為 /home/apps/1,建立檔案 /home/apps/protected/commands/crons.php
- $yii = '/home/apps/framework/yii.php';
- require_once($yii);
- $configFile = dirname(__FILE__).'/../config/console.php';
- Yii::createConsoleApplication($configFile)->run();
複製代碼
2,建立需要的設定檔 /home/apps/protected/config/console.php,配置需要的組件、資料庫連接,日誌等資訊,格式類似主設定檔main.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的各種方法。
- class TestCommand extends CConsoleCommand
- {
- public function run()
- {
- ...
- }
- }
複製代碼
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教程
|