Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic operation of php every day

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to run php automatically every day". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to run php automatically every day.

Php to achieve daily automatic operation methods: 1, install crontab;2 on the server, view the crontab timed execution task list; 3, according to the time setting rules can be set.

This article operating environment: linux5.9.8 system, PHP7.1 version, Dell G3 computer.

Php can not be executed regularly every day, only java or c can, because php does not visit the page will not move the code, but there is a way to save the nation

You write a page that you want to execute regularly every day, and then, depending on your operating system, do scheduled tasks and visit the page regularly. This paper takes the linux system as an example.

PHP implements the execution of scheduled tasks

First use the command to check if the service is running systemctl status crond.service if crontab is not installed on the server Then you can execute yum install vixie-cronyum install crontabs common instructions systemctl restart crond.service # start service systemctl reload crond.service # reload configuration systemctl status crond.service # View crontab service status systemctl enable crond.service # enter instructions (press I to edit) crontab-u root-e # enter scheduled task edit view crontab scheduled execution task list crontab-l basic format of related parameters : * command hourly, daily, monthly, weekly command column 1 represents minutes 1: 59 every minute by * or * / 1 (or by * / 1 per minute / n means every n minutes. For example, * / 8 means every 8 minutes. The second column represents the hour 1: 23 (0 for 0 point) the third column represents the date 1: 31 column 4 represents the month January 12 column 5 identification number week 0006 (0 means Sunday) column 6 commands to be run (if there are a lot of mailed 33 bytes of output bu get status 0x004b#012 please move to: https://bbs.aliyun.com/simple/ T135851.html): tail-f / var/log/cron example * / 2 * curl-o / home/index.html www.baidu.com # accesses www.baidu.com using curl every two minutes and writes the result to the / home/index.html file 00 * lynx-dump https://www.yourdomain.com/script.php # the current time is 00:00 minutes Perform the scheduled task lynx-dump https://www.yourdomain.com/script.php # to access the url through lynx. In our use, we mainly use lynx, curl and wget to achieve remote access to url. If we want to improve efficiency, directly using php to execute local php files is the best choice. 00 * / 2 * / usr/bin/php / home/www/script.php # directly use php to execute local php files

This statement can be executed every 0 minutes every 2 hours through the linux internal php environment. Note that it is not accessed through url and executed through the server environment, but directly. Because it bypasses the server environment, it is of course much more efficient.

As to why you want to use / usr/bin/php, you can use the command: which php or whereis php to view

In crontab, the result of the execution is output to a file. Then check the implementation in this file. For example:

* / 1 * / usr/bin/php / data/dou/web/api/cron.php > > / data/dou/web/cron.txt

Execute commands continuously:

Execute sequentially (separated by `;`)

Cmd1;cmd2;cmd3

According to logic and execution (& & separate)

Cmd1&&cmd2&&cmd3;cmd4

By logic or execution: (| | separate)

Cmd1 | | cmd2 | | cmd3;cmd4

That is, when the return value is 0, the logic will continue to execute, and if the return value is not 0, the logic or continue execution will be the same as the usual logic and logic or short circuit (`?` is the return value; if the command execution is correct, the return value will be 0 through echo $? Can be tested, specifically search "return")

Combination:

Cmd1

Cmd2 | | cmd3&&cmd4

Cmd5

Execute cmd1 first, then cmd2, cmd3 if the cmd2 echo value is non-0, cmd4 if the cmd3 echo value is 0, and then continue to execute sequentially

Example of time setting rules:

1. The rule is executed on a regular basis every minute:

Execute every 1 minute: * / 1 * or * *

Every 5 minutes: * / 5 *

two。 The rules are executed on a regular basis every hour:

Hourly execution: 0 * or 0 * / 1 *

Every day at 7: 00 a. M.: 0 7 *

Executed at 07:10 every morning: 10 7 *

3. The rules are executed regularly once a day:

Perform 0 0 * every day

4. The rules are executed regularly once a week:

Execute 0 0 * 0 every week

5. The rules are executed regularly once a month:

Execute 0 01 * * every month

6. The rules are enforced regularly once a year:

0 0 1 1 * per year

7. Other examples

5 * specify the ls command to be executed at the 5th minute of every hour

305 * ls specifies that the ls command be executed at 5:30 every day

30 7 8 * * ls specifies that the ls command will be executed at 7:30 on the 8th of each month

30 5 8 6 * ls specifies that the ls command will be executed at 5:30 on June 8 of each year

306 * * 0 ls specifies that the ls command be executed at 6:30 every Sunday [Note: 0 for Sunday, 1 for Monday, and so on, and so on, can also be expressed in English, sun for Sunday, mon for Monday, etc.]

30 3 10 ls 20 * * execute the ls command at 3:30 on the 10th and 20th of each month [Note: "," used to connect multiple discontinuous periods]

25 8-11 * ls executes the ls command at the 25th minute of 8-11:00 every day [Note: "-" used to connect continuous periods of time]

* / 15 * ls executes the ls command every 15 minutes [that is, the ls command is executed on the 015 30 45 60 minute of each hour]

306 * / 10 * * ls executes the ls command at 6:30 every 10 days [that is, at 6:30 on the 1st, 11th, 21st and 31st of each month. ]

At this point, I believe you have a deeper understanding of "how to automatically run php every day". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report