An example of creating a Cronjob timed task in Yii

Source: Internet
Author: User
Tags yii
This article mainly introduces the method of creating Cronjob timed task in Yii framework, and analyzes the related configuration, implementation steps and matters needing attention of Yii timing task with concrete instance form, and the friends who need can refer to

The example in this paper describes how the YII framework creates cronjob timed tasks. Share to everyone for your reference, as follows:

1. Add Environment Configuration

protected/config/console.php

<?phprequire_once (' env.php ');//The configuration for YIIC console application.//any writable Cconsoleapplica tion properties can be configured Here.return array (  ' BasePath ' =>dirname (__file__). Directory_separator. '. ',  ' name ' = ' CMS Console ',  //Application Components  ' components ' =>array (    //main DB connection    ' db ' =>array (      ' connectionString ' =>db_connection,      ' username ' =>db_ USER,      ' password ' =>db_pwd,      ' enableparamlogging ' =>true, '    log ' =>array (      ' class ') = ' Clogrouter ',      ' routes ' =>array (        Array (          ' class ' = ' Cfilelogroute ',          ' levels ' = ') Error, warning ',),),),)  ;

2. Adding a timed Task execution module

protected/commands/crons.php

<?phpdefined (' yii_debug ') or define (' Yii_debug ', true);//Including yiirequire_once ('/... /framework/yii.php ');//We'll use a separate config file$configfile= '/config/console.php ';//Creating and running Console Applicationyii::createconsoleapplication ($configFile)->run ();

3. Add a specific scheduled task

A timed task is usually a command-line program that derives from the Cconsolecommand class, such as
protected/commands/testcommand.php

Class Testcommand extends cconsolecommand{public  function Run ($args) {    //todo  }}

4. Create Cronjob

0 * * * www php/path/to/crons.php Test >>/path/to/logs/test.log

5. Pass parameters to run ($params) in the scheduled task

0 * * * www php/path/to/crons.php Test param1 param2 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.