PHP timed to execute PHP script some method summary

Source: Internet
Author: User
Tags php script sleep root directory

Linux executes PHP scripts on a regular basis


Execute PHP Script

Method 1
If you want to execute a PHP script on a regular basis, you can write it directly:
*/1 * * * * * root-q/bin/local/php/path/to/your/php/script.php (script.php per minute)
Note that you need to turn PHP into an executable script file. At the command line, the directory where the script resides
chmod +x script.php

crontab+php-cgi Execute PHP scripts regularly

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

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

2. Create crontab files

2.1 Crontab-e

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

#编辑crond计划, request index.php per minute

Completed, the above steps are completed, in my computer can be normal per minute request index.php


Precautions:

One./usr/local/webserver/php/is my PHP installation path

Two. php-cgi is the PHP parser (my understanding is that, I always feel that I understand the wrong, I hope the prawns are correct), some information is used./bin/php, I can not on the computer, so with php-cgi, may install the wrong way.

Three. Q is quiet mode (see in some data) run PHP script

Four. Some of the information is to add #! to the first line in index.php /usr/local/webserver/php/bin/php-q, and add execute permissions to index.php. I didn't do it, and I was able to request index.php.


Finally, the implementation of a PHP timed execution task is introduced

Functions used Ignore_user_abort (), Set_time_limit (0), Sleep ($interval)
This code closes the browser as soon as it is run once.

  code is as follows copy code

Ignore_ User_abort ()//Turn off the browser, the PHP script can also continue to execute.
Set_time_limit (0);//Through Set_time_limit (0) allows the program to execute indefinitely
$interval =60*30;//run
do{
every half hour  // Here is the code you want to execute &NBSP
 sleep ($interval);/wait 5 minutes
}while (true);

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.