Assume the Yii project path is /home/apps/
1. Create file /home/apps/protected/commands/crons.php
<?<span php </span><span $yii</span> = '/home/apps/framework/yii.php'<span ; </span><span require_once</span>(<span $yii</span><span ); </span><span $configFile</span> = <span dirname</span>(<span __FILE__</span>).'/../config/console.php'<span ; Yii</span>::createConsoleApplication(<span $configFile</span>)->run();
2. Create the required configuration file /home/apps/protected/config/console.php, configure the required components, database connections, logs and other information. The format is similar to the main configuration file main.php
<?<span php </span><span return</span> <span array</span><span ( </span>'basePath'=><span dirname</span>(<span __FILE__</span>).DIRECTORY_SEPARATOR.'..', 'name'=>'Emergency', 'import'=><span array</span><span ( </span>'application.models.*', 'application.components.*', 'application.extensions.*',<span )</span>, 'components'=><span array</span><span ( </span>'log'=><span array</span><span ( </span>'class'=>'CLogRouter', 'routes'=><span array</span><span ( </span><span array</span><span ( </span>'class'=>'CFileLogRoute', 'levels'=>'info, warning, error',<span )</span>,<span )</span>,<span )</span>, 'db'=><span array</span><span ( </span>'class'=>'application.extensions.PHPPDO.CPdoDbConnection', 'pdoClass' => 'PHPPDO', 'connectionString' => 'mysql:host=xxxx;dbname=xxx', 'emulatePrepare' => <span true</span>, 'username' => 'xxx', 'password' => 'xxx', 'charset' => 'utf8', 'tablePrefix' => 'tbl_',<span )</span>,<span )</span>, 'params' => <span require</span>('params.php'),<span );</span>
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
<?<span php </span><span class</span> TestCommand <span extends</span><span CConsoleCommand { </span><span public</span> <span function</span><span run() { </span>...<span } }</span>
4. Create scheduled tasks
$ crontab -e
Insert
1 * * * * /home/php/bin/php -f /home/apps/<span protected</span>/commands/crons.php Test &
means 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 execute them using the command line.