Yii2 commands mode and how to configure a crontab scheduled task, yii2crontab
I. Detection Environment:
First, switch to the project root directory. If yii2 is properly installed, there is a commands folder, which contains a sample file HelloController. php.
<?phpnamespace 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, run the command line to output php yii hello, and output hello world to indicate that the environment is normal.
2. write code:
You can create a Controller file in the commands folder and inherit yii \ console \ Controller; Define the class method. The actionIndex method is generally the default route,
During debugging, enter php yii in the project root directory with the Controller name (lower case)/route (index can be omitted)
3. linux scheduled task crontab.
1 about crontab:
In linux, crontab-l displays scheduled tasks and crontab-e edits scheduled tasks.
2. Basic syntax
Basic Format:
* *** Command
Hour, day, month, and week commands
The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run
3 usage example
30 21 ***/usr/local/etc/rc. d/lighttpd restart
# The example above indicates restarting apache at every night.
45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
# The example above indicates that apache is restarted at every month on the 1st, 10th, and 22th.
10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart
# The example above indicates restarting apache every Saturday and Sunday.
0, 30 18-23 ***/usr/local/etc/rc. d/lighttpd restart
# The example above indicates that apache is restarted every 30 minutes between and every day.
0 23 ** 6/usr/local/etc/rc. d/lighttpd restart
# The example above indicates that apache is restarted at every Saturday.
0 */1 ***/usr/local/etc/rc. d/lighttpd restart
# Restart apache every hour
4. Run the linux scheduled task crontab to execute the Controller PHP file in commands.
As written in step 3, add php yii + project root path + route (Controller name/method name ).
The above yii2 commands mode and the method for configuring crontab scheduled tasks are all the content that I have shared with you. I hope to give you a reference and support for the help house.