Several ways to implement timed tasks in PHP

Source: Internet
Author: User
Tags script php

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

Related Article

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.