This article describes how to create a scheduled task by using commands in the yii framework on the console. For more information, see
This article describes how to create a scheduled task by using commands in the yii framework on the console. For more information, see
Assume that the Yii project path is/home/apps/
1. Create a file/home/apps/protected/commands/crons. php
The Code is as follows:
$ Yii = '/home/apps/framework/yii. php ';
Require_once ($ yii );
$ ConfigFile = dirname (_ FILE _). '/../config/console. php ';
Yii: createConsoleApplication ($ configFile)-> run ();
2. Create the required configuration file/home/apps/protected/config/console. php to configure the required components, database connections, logs, and other information. The format is similar to the main. php configuration file.
The Code is as follows:
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR .'..',
'Name' => 'ergency ',
'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 ',
'Default' => 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 from CConsoleCommand. In TestCommand, you can use the configuration information of the project and various methods of Yii.
The Code is as follows:
Class TestCommand extends CConsoleCommand
{
Public function run ()
{
...
}
}
4. Create a scheduled task
The Code is as follows:
$ Crontab-e
Insert
The Code is as follows:
1 ***/home/php/bin/php-f/home/apps/protected/commands/crons. php Test &
That is, the content in the TestCommand class is executed in the first minute of every hour. Similarly, you can create other classes under/home/apps/protected/commands/and execute them using the command line.