让PHP程序永远在后台运行

PHP里有个函数很有用。这是在最近的开发中才逐渐用到的。
int ignore_user_abort ( [bool setting] )
这个函数的作用是指示服务器端在远程客户端关闭连接后是否继续执行下面的脚本。

setting 参数是一个可选参数。如设置为True,则表示如果用户停止脚本运行,仍然不影响脚本的运行(即:脚本将持续执行);如果设置为False,则表示当用户停止运行脚本程序时,脚本程序将停止运行。

下面这个例子,在用户关闭浏览器后,该脚本仍然后在服务器上继续执行:

  1. <?php
  2. ignore_user_abort(); // 后台运行
  3. set_time_limit(0); // 取消脚本运行时间的超时上限
  4. do{
  5. sleep(60); // 休眠1分钟
  6. }while(true);
  7. ?>


除非在服务器上关闭这个程序,否则这断代码将永远执行下去。

-------------------------------------------------------------------------

  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. ?>




原文地址:https://www.cnblogs.com/lookphp/p/5276530.html