Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework

Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 09:41:491868browse

This time I will bring you a detailed explanation of the steps to create a scheduled task through the console command in the yii framework. What are the precautions for creating a scheduled task through the console command in the yii framework. The following is a practical case, let’s take a look. one time.

Assume that the Yii project path is /home/apps/

1, create the file /home/apps/protected/commands/crons.php

run();

2, create the required

Configuration file /home/apps/protected/config/console.php, configure required components, database connections, logs and other information, the format is similar to the main configuration file main.php.

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. Create a new TestCommand class under /home/apps/protected/commands/ and inherit CConsoleCommand. In TestCommand, you can use the project's configuration information and Yii's various methods.


4, create a scheduled task

$ crontab -e
The content is:

1 * * * * /home/php/bin/php -f /home /apps/protected/commands/crons.php Test & is to execute the contents of the TestCommand class in the first minute of every hour. Similarly, you can create other classes under /home/apps/protected/commands/ and use the command Execute.

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the use case of volist tag in thinkphp

Detailed explanation of the steps for printing binary trees in Z-shaped order in PHP

thinkWhat are the precautions for using the volist tag in ajax in thinkphp

The above is the detailed content of Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework. For more information, please follow other related articles on the PHP Chinese website!

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