Php scheduled execution of PHP scripts-PHP Tutorial

Source: Internet
Author: User
Php regularly executes PHP scripts. This article summarizes some methods for regularly executing php scripts in PHP. For more information, see. Scheduled execution of php scripts in linux P This article summarizes some methods for php scheduled execution of PHP scripts, including the methods for linux, windows, and php, for more information, see.

Scheduled execution of php scripts in linux


Execute PHP scripts

Method 1
If you want to regularly execute a PHP script, you can directly write it like this:
*/1 * root-q/bin/local/php/path/to/your/php/script. php (execute script. php every minute)
Note: In this case, PHP needs to be converted into executable script files. In the command line, the directory where the script is located
Chmod + x script. php

Crontab + php-cgi: regularly execute PHP scripts

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

My file path (varies 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 the crond plan and request index. php every minute

After completing the preceding steps, you can request index. php every minute on your computer.


Note:

I./usr/local/webserver/php/is my php Installation Path

II. php-cgi is a php parser (my understanding is like this, I always feel that I do not understand it, I hope the prawn correction), some information is used. /bin/php cannot be installed on my computer, so it may be wrong to use php-cgi.

3.-q is in quiet mode (as shown in some documents). run the php script.

4. some materials are added to the first line of index. php #! /Usr/local/webserver/php/bin/php-q and add the execution permission to index. php. If this is not done, index. php can be requested normally.


Finally, the implementation of a PHP scheduled task is introduced.

Functions used: ignore_user_abort (), set_time_limit (0), sleep ($ interval)
This code only needs to close the browser once it runs.

The code is as follows:

Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program
$ Interval = 60*30; // run every 30 minutes
Do {
// Here is the code you want to execute
Sleep ($ interval); // wait for 5 minutes
} While (true );


Bytes. In linux, execute php scripts regularly to execute P...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.