How to run php automatically every day
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
