The method of adding the PHP file to execute the scheduled task in Linux system

Time:2020-1-14

In the process of web development, it is often necessary to set some tasks to be performed on a regular basis, such as regular goods loading and unloading. We take PHP file as an example to explain the planned tasks under Linux.

Method / step

1. Open the command line interface of Linux system.

在linux系统下添加计划任务执行php文件

In the command line interface, enter the following command:which phpReturn.

在linux系统下添加计划任务执行php文件

1. Remember the PHP path in the previous step, and continue to enter:Crontab-e, enter, press “I” to enter the editing mode of VI editor, enter the following command in the current mode:

10,40,50 * * * * /usr/bin/php /opt/lampp/htdocs/wechat/xjtest-web_browser/pcntl_test.php

That is: scheduled task execution frequency + PHP executable path + PHP file path to be executed. (at the end of the article, the execution frequency of the command will be explained in detail.)

在linux系统下添加计划任务执行php文件

When the input is complete, press the “ESC” key and enter it on the current command line“:”, enter “WQ” after “:”, press enter again, and the following prompt will appear to prove that the planned task is added successfully.

在linux系统下添加计划任务执行php文件

Linux scheduled task execution frequency:

The frequency of the command start part is composed of five parts, representing minute, hour, day, month and week (0 is Sunday). When the current frequency is all executed, it is indicated by “*”, so there are so many “*”. The following examples can help you understand:

The following scheduled tasks will run all the time

* * * * * [command]

The following scheduled tasks will run at 0:00 an hour (i.e. once an hour)

0 * * * * [command]

The following is still an hourly scheduled task. But it runs at a quarter of an hour (1:15, 2:15.)

15 * * * * [command]

The following scheduled tasks are performed once a day at 2:30

30 2 * * * [command]

The following scheduled tasks are performed at midnight on the 2nd of each month (for example: 00:00 on February 2, 2010)

0 0 2 * * [command]

The following scheduled tasks will be performed at 0 minutes per hour on Monday

0 * * * 1 [command]

The following will run three times an hour at 0, 10, and 20

0,10,20 * * * * [command]

The following is a division sign “/” to indicate how many times to run. This example runs every 5 minutes

*/5 * * * * [command]

You can also use “-” to indicate a range, as shown below, for each whole point from 5 to 10 points to run once

0 5-10 * * * [command]

In addition, there is a special time keyword “@ reboot”, which is to run the scheduled task every time the server is restarted

@reboot [command]

The above is the method of adding planned tasks to execute PHP files under Linux system. Interested users are welcome to follow developepaer!