1. Detection environment:
First we switch to the project root directory. If yii2 is installed normally, there will be a commands folder with a sample file HelloController.php in it.
<?php namespace app\commands;use yii\console\Controller;class HelloController extends Controller { public function actionIndex($message = 'hello world') { echo $message . "\n"; } }
Switch to the project Root directory, command line output php yii hello, output hello world indicating that the environment is normal
2. Write code:
You can create a new controller file in the commands folder and inherit yii\console \Controller; Define class method, actionIndex method is generally the default route,
When debugging, enter php yii in the project root directory and add the controller name (lowercase)/routing (index can be omitted )
3. Linux scheduled task crontab.
1 About crontab:
In the linux environment, crontab -l displays scheduled tasks, crontab -e edits scheduled tasks
2 Basic syntax
Basic format:
* * * * * command
Time-sharing, day, month and week command
Column 1 Indicates minutes from 1 to 59. Each minute is represented by * or */1
. The second column indicates hours from 1 to 23 (0 indicates 0 o'clock)
. The third column indicates the date from 1 to 31
. The fourth column indicates the month. 1~12
around 5 Number 5 weekdays 0 ~ 6 (0 means Sunday)
The command to be run in the 6th column #30
21
* * * /usr/local/etc/rc.d/lighttpd restart# #The above example indicates that apache is restarted at 21:30 every night.
45 4
1
,10,22 * * /usr/local/etc/rc.d/lighttpd restart #The above example indicates that apache is restarted at 4:45 on the 1st, 10th, and 22nd of every month.
101
**
6 ,0 /usr/local/etc/rc.d/lighttpd restart # #The above example represents 1 of every Saturday and Sunday : 10Restart apache. 0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart 23 * * 6 /usr/local/etc/rc.d/lighttpd restart# #The above example indicates that apache is restarted at 11:00 pm every Saturday. 0 */1 * * * /usr/local/etc/rc.d/lighttpd restart ## #Restart apache every hour Four, Linux scheduled task crontab executes the controller php file content under commands Follow the third step and add php yii + project root path + routing ( Controller name/method name)
The above is the detailed content of Commands mode in yii2 and crontab scheduled tasks under configuration. For more information, please follow other related articles on the PHP Chinese website!