In Windows, can Apache execute a php file every few seconds?-php Tutorial

Source: Internet
Author: User
In Windows, can Apache execute a php file every few seconds? please allow me to ask this very idiotic question, but I haven't thought of a good solution for a long time. The background is as follows: I need a piece of PHP code to perform regular operations on the database data and save the results. If the code is executed only when the user requests it, the user's response time will be long and the user experience will be poor. So I want to have a program dedicated to executing this code. In Windows, can Apache execute a php file every several seconds?
Please allow me to ask this silly question, but I haven't thought of a good solution for a long time.
The background is as follows: I need a piece of PHP code to perform regular operations on the database data and save the results. If the code is executed only when the user requests it, the user's response time will be long and the user experience will be poor. So I want to have a program dedicated to executing this code. At the beginning, I thought of using windows to schedule tasks. However, because the execution frequency is very high, it is obviously not suitable to use scheduled tasks.
So I hope you can give me some advice. it would be better to give a simple example. thank you!

------ Solution --------------------
It seems that you can only use scheduled tasks or PHP for execution.
PHP code
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 do every half an hour {// Here is the code you want to execute sleep ($ interval); // wait 5 minutes} while (true );
------ Solution --------------------
Linux has cron, which is easy to use.

If there is Event Scheduler in mysql5.1 or above in windows, but it has never been used, LZ will check the information.

------ Solution --------------------
Multiple methods:
1. use the sleep () function to implement scheduled execution similar to linux contab.

2. in linux, add scheduled tasks directly in crontab.
------ Solution --------------------
You can make a file that contains the first floor, and then include the file in the desired place. This saves you the need to use resources and runs as you call the file. how nice is it, after the web page is closed, you can continue running. Any action can be triggered. It is better to trigger with load.
------ Solution --------------------
Use js to regularly refresh the webpage and read the database from the webpage. As long as the web page is opened, it can always run. It also occupies less resources.
------ Solution --------------------
Use js to refresh a page.

Set the browser's homepage to this page.

Add the browser to windows startup.




------ Solution --------------------
Write test script:

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 = 30; // Run at intervals
Do {

// Here is the code you want to execute
$ Msg = date ("Y-m-d H: I: s"). "-executed ~ \ N ";
File_put_contents ("log. log", $ msg, FILE_APPEND );

Sleep ($ interval); // wait for 5 minutes
} While (true );


Then you can directly execute
C: \ php5 \ php.exe test. php

You can add it to start running or to system services to automatically run it.

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.