Home  >  Article  >  Backend Development  >  How to run php automatically every day

How to run php automatically every day

藏色散人
藏色散人Original
2021-12-30 09:34:263942browse

How to realize automatic running of php every day: 1. Install crontab on the server; 2. View the crontab scheduled execution task list; 3. Set according to the time setting rules.

How to run php automatically every day

The operating environment of this article: linux5.9.8 system, PHP version 7.1, Dell G3 computer.

php cannot be executed regularly every day, only java or c can, because php will not move the code without accessing the page, but there is a way to save the country

You write a page It is a page that you want to execute regularly every day, and then depending on your operating system, do a scheduled task and visit this page regularly. This article takes the Linux system as an example.

PHP implements scheduled tasks

First use the command to check whether the service is running

systemctl status crond.service

If crontab is not installed on the server, then You can execute

yum install vixie-cron
yum install crontabs

Common commands

systemctl restart crond.service #启动服务 systemctl reload crond.service #重新载入配置 systemctl status crond.service #查看crontab服务状态 systemctl enable crond.service #开机自启动

After starting the service, you can enter the command (press i to edit)

crontab -u root -e #进入定时任务编辑

View the crontab scheduled execution task list

crontab -l

Related parameters

基本格式 :  
*    *  *   *   *  command  
分  时  日  月  周   命令  
第1列表示分钟1~59 每分钟用*或者 */1表示 (每分钟用或者 */1表示,/n表示每n分钟,例如*/8就是每8分钟的意思,下面也是类推)
第2列表示小时1~23(0表示0点)  
第3列表示日期1~31  
第4列表示月份1~12  
第5列标识号星期0~6(0表示星期天)  
第6列要运行的命令

Check the dynamic log (if there are many 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 #每隔两分钟使用curl 访问www.baidu.com 并将结果写入/home/index.html 文件
00 * * * * lynx -dump https://www.yourdomain.com/script.php   #当前时间的分钟数为00时,执行该定时任务
lynx -dump https://www.yourdomain.com/script.php  #通过lynx访问这个url。我们在使用中主要用到lynx、curl、wget来实现对url的远程访问,而如果要提高效率,直接用php去执行本地php文件是最佳选择
00 */2 * * * /usr/bin/php /home/www/script.php  #直接用php去执行本地php文件

This statement can execute script.php through the Linux internal php environment at 0 minutes every 2 hours. Note that this is not accessed through url, but executed through the server environment. Oh, but it is executed directly, because the server environment is bypassed, so the efficiency is of course much higher.

As for why you need to use /usr/bin/php, you can use the command: which php or whereis php View

In crontab, output the execution results to a file. Then check the execution status in this file. For example:

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

Continuous execution of commands:
Execute in order (separated by `;`)

cmd1;
cmd2;
cmd3;

Execute by logic and (separated by && )

cmd1&&cmd2&&cmd3;
cmd4;

Execute by logical OR: (||separated)

cmd1||cmd2||cmd3;
cmd4;

That is, when the returned value is 0, the logical AND will continue to be executed and the returned value If it is not 0, the logical OR will continue to execute. The logic of the normal program is the same as the logical OR short-circuit (`?` is the return value. When the command is executed correctly, the return value is 0. You can test it through echo $?. Search for "return" ")
combination:
cmd1;

cmd2||cmd3&&cmd4;

cmd5;
Execute cmd1 first and then cmd2. If the value returned by cmd2 is not 0, execute cmd3, if the return value of cmd3 is 0, execute cmd4, and then continue to execute in sequence

Time setting rule example:

1. Execute the rule regularly every minute :
Execute every 1 minute: */1 * * * *or * * * * *
Execute every 5 minutes: */5 * * * *

2. Execute regularly every hour Rules:
Hourly execution: 0 * * * * or 0 */1 * * *
Daily execution at 7 am: 0 7 * * *
Daily execution at 7:10 am: 10 7 * * *

3. Execute the rule regularly every day:
Execute every day 0 0 * * *

4. Execute the rule regularly every week:
Execute every week 0 0 * * 0

5. Execute the rule regularly every month:
Execute 0 0 1 every month * *

6. Execute the rule regularly every year:
Execute 0 0 1 every year 1 *

7. Other examples
5 * * * * Specify the ls command to be executed at the 5th minute of every hour
30 5 * * * ls Specify the ls command to be executed at 5:30 every day
30 7 8 * * ls specifies the ls command to be executed at 7:30 on the 8th of each month
30 5 8 6 * ls specifies the ls command to be executed at 5:30 on June 8 of each year
30 6 * * 0 ls specifies to execute the ls command at 6:30 every Sunday [Note: 0 means Sunday, 1 means Monday 1, and so on. It can also be expressed in English, sun means Sunday, mon means Monday, etc. ]
30 3 10,20 * * ls Execute the ls command at 3:30 on the 10th and 20th of each month [Note: "," is used to connect multiple discontinuous time periods]
25 8-11 * * * ls Execute the ls command at the 25th minute from 8 to 11 o'clock every day [Note: "-" is used to connect consecutive periods]
*/15 * * * * ls Execute the ls command every 15 minutes [i.e. every Execute the ls command at 0, 15, 30, 45 and 60 minutes of the hour]
30 6 */10 * * ls Execute the ls command at 6:30 every 10 days in each month [i.e. 1, 11, and 21 of each month , execute the ls command once at 6:30 on the 31st. ]

【Recommended learning: PHP video tutorial

The above is the detailed content of How to run php automatically every day. 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