Home > Backend Development > PHP Tutorial > Summary of some methods for php to execute PHP scripts regularly_PHP Tutorial

Summary of some methods for php to execute PHP scripts regularly_PHP Tutorial

WBOY
Release: 2016-07-20 11:11:37
Original
784 people have browsed it

This article summarizes some methods for regularly executing PHP scripts in PHP. There are methods in Linux, Windows, and PHP itself. Friends in need can refer to them.

Execute PHP script regularly under Linux


Execute PHP script

Method 1
If you want to execute a PHP script regularly, you can write it directly like this:
* /1 * * * * root -q /bin/local/php /path/to/your/php/script.php (execute script.php every minute)
Note that writing this way requires making PHP accessible Script file to execute. Under the command line, the directory where the script is located
chmod +x script.php

crontab+php-cgi executes the PHP script regularly

1. Create the requested script index in the web root directory. php

My file path (different paths depending on the installation path): /usr/local/webserver/htdosc/index.php

2. Create a crontab file

2.1 crontab -e

2.2 * * * * * /usr/local/webserver/php/bin/php-cgi -q /usr/local/webserver/htdosc/index.php

#Edit crond plan, request index.php every minute

is completed. After the above steps are completed, my computer can normally request index.php every minute


Notes:

1. /usr/local/webserver/php/ is my php installation path

2. php-cgi is the php parser (this is my understanding, but I always feel that I understand it wrong , hope you guys can correct me), some information is using ./bin/php, which is not available on my computer, so I use php-cgi, maybe the installation method is wrong.

3. -q is the quiet mode (seen in some materials) to run the php script

4. Some materials add #! to the first line of index.php /usr/ local/webserver/php/bin/php -q and add execution permissions to index.php. I didn't do this, and I could request index.php normally


Finally, I will introduce the implementation of a PHP scheduled execution task

The functions used ignore_user_abort(),set_time_limit(0),sleep ($interval)
You only need to run this code once and then close the browser.

The code is as follows
 代码如下 复制代码

ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去
$interval=60*30;// 每隔半小时运行
do{
 //这里是你要执行的代码 
 sleep($interval);// 等待5分钟
}while(true);

Copy code
ignore_user_abort();//Close the browser, and the PHP script can continue to execute.
set_time_limit(0);//Through set_time_limit(0), the program can be executed without limit$interval=60*30;//Run every half hour

do{

//Here is the code you want to execute } while(true);
http://www.bkjia.com/PHPjc/444643.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/444643.htmlTechArticleThis article summarizes some methods for php to execute PHP scripts regularly. There are methods for linux, windows and php itself. , friends in need can refer to it. Execute PHP script regularly under Linux. Execute P...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template