Home >Backend Development >PHP Tutorial >yii2 configure crontab scheduled tasks

yii2 configure crontab scheduled tasks

大家讲道理
大家讲道理Original
2017-08-19 09:35:343622browse

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";
    }
}

Cut to the project root directory, command line output php yii hello, output hello world indicating that the environment is normal


2. Write the code:

You can Create a new controller file in the commands folder, inherit yii\console\Controller; define class methods, the actionIndex method is generally the default route,

When debugging, enter php yii in the project root directory Add the controller name (lowercase)/route (can be omitted if index)

Third, Linux scheduled task crontab.

1About 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 commands

The first column represents minutes 1 to 59. Each minute is represented by * or */1

The second column represents hours 1 to 23 (0 represents 0 o'clock)

The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th column identifies the weekday 0~6 (0 means Sunday)
The 6th column represents the command to be run

3 Usage examples

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.

 10

1

* * 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

##   #The above example indicates that between 18:00 and 18:00 every day Restart apache every 30 minutes between 23:00.

 0

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 yii2 configure crontab scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn