Home >Backend Development >PHP Tutorial >How does the yii framework create scheduled tasks through console commands?

How does the yii framework create scheduled tasks through console commands?

WBOY
WBOYOriginal
2016-07-25 09:12:56963browse

Assume that the Yii project path is /home/apps/ 1. Create the file /home/apps/protected/commands/crons.php

  1. $yii = '/home/apps/framework/yii.php';
  2. require_once($yii);
  3. $configFile = dirname(__FILE__).'/../config/ console.php';
  4. 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.

  1. return array(

  2. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
  3. 'name'=>'Emergency',
  4. 'import'=>array(
  5. 'application.models.*',
  6. 'application.components.*',
  7. 'application.extensions.*',
  8. ),
  9. 'components'=>array(
  10. 'log '=>array(
  11. 'class'=>'CLogRouter',
  12. 'routes'=>array(
  13. array(
  14. 'class'=>'CFileLogRoute',
  15. 'levels'=>'info, warning, error',
  16. ),
  17. ),
  18. ),
  19. 'db'=>array(
  20. 'class'=>'application.extensions.PHPPDO.CPdoDbConnection',
  21. 'pdoClass' => 'PHPPDO' ,
  22. 'connectionString' => 'mysql:host=xxxx;dbname=xxx',
  23. 'emulatePrepare' => true,
  24. 'username' => 'xxx',
  25. 'password' => 'xxx' ,
  26. 'charset' => 'utf8',
  27. 'tablePrefix' => 'tbl_',
  28. ),
  29. ),

  30. 'params' => require('params. php'),

  31. );

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.

  1. class TestCommand extends CConsoleCommand
  2. {
  3. public function run()
  4. {
  5. ...
  6. }
  7. }
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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn