Keep PHP programs running in the background forever

Source: Internet
Author: User

There is a function in PHP that is useful. This was only gradually used in the recent development. The function of int ignore_user_abort ([bool setting]) is to indicate whether the server side continues to execute the following script after the remote client closes the connection.
The setting parameter is an optional parameter. If set to True, indicates that the script will not be affected if the user stops running the script (that is, the script continues to execute), and if set to false means that the script will stop running when the user stops running the script.
In the following example, after the user closes the browser, the script continues to execute on the server:
  
 
  1. <?php
  2. ignore_user_abort(); // 后台运行
  3. set_time_limit(0); // 取消脚本运行时间的超时上限
  4. do{
  5. sleep(60); // 休眠1分钟
  6. }while(true);
  7. ?>


This code will be executed forever unless the program is closed on the server.
-------------------------------------------------------------------------
  
 
  1. <?php
  2.    ignore_user_abort(); // 后台运行
  3.    set_time_limit(0); // 取消脚本运行时间的超时上限
  4.    echo ‘start.‘;
  5.    while(!file_exists(‘close.txt‘)){
  6.     $fp = fopen(‘test.txt‘,‘a+‘);
  7.     fwrite($fp,date("Y-m-d H:i:s") . " 成功了!rn");
  8.     fclose($fp);
  9.     sleep(10);
  10.    }
  11.    echo ‘end.‘;
  12. ?>


From for notes (Wiz)

Keep PHP programs running in the background forever

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.