Yii2 commands mode and how to configure a crontab scheduled task, yii2crontab

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.