[PHP Learning Tutorial]002. Simulation Daemon (Daemon)-The program is always running in the background

Source: Internet
Author: User

Introduction: How to simulate those automatic round-robin services, like the daemon (Daemon) , can be executed, never stop!

Come on! Do it! Do! Do! Do!....

Using interfaces:

int ignore_user_abort ([bool setting])

Method declaration:

The purpose of this function is to indicate whether the server side continues to execute the following script after the remote client closes the connection.

Parameter description:

Setting: parameter is an optional parameter. If set to True, this means that if the user stops the script, it still does not affect the script's run (that is, the script will continue to execute), and if set to False, indicates that when the user stops running the script, The scripting program will stop running .

Speaking so much, is not as direct as a line of code to effectively, directly engaged.

Basic example

In the following example, after the user closes the browser , the script continues to execute on the server:

<?PHP//running in the backgroundIgnore_user_abort();//maximum timeout for canceling script run timeSet_time_limit(0);//perform round robin Do {    //sleep for 1 minutes    Sleep(60); //dealing with specific things    Echo' Do! ';}  while(true);?>

Description: This code will be executed forever unless the program is closed on the server.

Example 1 for extended display

Mark, we are interested to have a look.

Implementation features: record time every 10 seconds to file, never stop.

<?PHP//running in the backgroundIgnore_user_abort();//maximum timeout for canceling script run timeSet_time_limit(0);//debug Log [start]Echo' Start. ';//perform round robin while(!file_exists(' Close.txt ')) {    //Open File    $handle=fopen(' Test.txt ', ' A + '); //Write Date    fwrite($handle,Date("Y-m-d h:i:s"). "It worked!" \ n "); //Close File    fclose($handle); //sleep for 10 seconds    Sleep(60);}//debug Log [end]Echo' End. ';?>

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4793943.html

[PHP Learning Tutorial]002. Simulation Daemon (Daemon)-The program is always running in the background

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.