Assume that the Yii project path is /home/apps/
1. Create the file /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();
Copy code
2, create the required configuration file /home/apps/protected/config/console.php, configure The required components, database connections, logs and other information are in a format similar to the main configuration file 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'),
- );
-
Copy code
3, create a new TestCommand class under /home/apps/protected/commands/, inherit CConsoleCommand, in TestCommand, you can use the project configuration Information and various methods of Yii.
-
-
- class TestCommand extends CConsoleCommand
- {
- public function run()
- {
- ...
- }
- }
Copy code
4, create a scheduled task
$crontab-e
The content is:
1 * * * * /home/php/bin/php -f /home/apps/protected/commands/crons.php Test &
That is, the contents of the TestCommand class are executed in the first minute of every hour. Similarly, other classes can be created under /home/apps/protected/commands/ and executed using the command line.
For the usage of crontab, please refer to:
- crontab command basics and examples
- Some examples of crontab commands
- Detailed explanation of Linux installation crontab
- crontab study notes
- Learn the crontab command to set up scheduled tasks in Linux
- crontab command format and examples
- Linux scheduled task setting crontab learning
- Good crontab tutorial
|