About timed tasks, previously known one of the most commonly used: Crontab timed tasks. It is implemented by Linux timed tasks. Today, I know another way to implement the timing mode of PHP, summed up a bit.
One server Scheduled task
Server timing task, in fact, is the UNIX system crontab implementation, specific settings: Linux timed task crontab; But in addition to the way we read PHP scripts directly, we can also invoke the interface in a timed manner.
When you run the script:
*/1 * * * * php/data/www/cron.php execute cron.php per minute
Url method call:
Lynx Mode: */1 * * * * lynx-dump http://www.gzpblog.com/cron.php (-dump option to convert the output of the URL to standard output)
Curl Mode: */1 * * * */usr/bin/curl-o temp.txt http://www.gzpblog.com/cron.php (Curl defaults to display output in standard output. Use the "curl-o" option to dump the output of the script to a temporary file temp.txt)
wget mode: */1 * * * */usr/bin/wget-q-o temp.txt http://www.gzpblog.com/cron.php (q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file. )
In this way, the PHP script is placed in an address location that can be accessed by URL, such as http://www.gzpblog.com/cron.php, which invokes a timed call to trigger the task.
Two ignore_user_abort () mode
The Ignore_user_abort () function sets the execution of the script to terminate if the client disconnects.
First use a cron.php file to control the termination of the script, cron.php content is:
<?phpreturn 1;? >
The script php file is:
<?phpignore_user_abort ();//switch off the browser, the PHP script can also continue to execute. Set_time_limit (0);//Through Set_time_limit (0) You can allow the program to execute indefinitely $interval =60*30;//run do{every half hour $run = include ' config.php ', if (! $run) die (' Process abort '); Return 0 o'clock, stop//todo What to do Sleep ($interval);//wait 5 minutes}while (true);
The STOP program is implemented by changing the return 0 of the cron.php. But this method is not good for memory management.
Three
File_get_contents () mode
<?php$time=15; $url = "http://". $_server[' http_host '].$_server[' Request_uri '];//todosleep ($time); file_get_ Contents ($url);? >
The PHP script continues to execute after a period of time by accessing itself, which guarantees that each PHP script execution time is not too long and is not subject to time_out restrictions. Because each time the PHP file is executed independently, this method avoids the time_out limitation. However, it is best to add the control code cron.php, as above, so that the process can be terminated.
Several ways to implement timed tasks in PHP