duansao6776 2014-08-28 16:10
浏览 72
已采纳

什么可能会不合需要地停止长期运行的PHP脚本?

If a long running php script is created in a LAMP stack, using:

set_time_limit(0);
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();

$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();            
session_write_close();

//Some long running code here

In what ways could this script subsequently be terminated externally (to the script itself)?

  • 写回答

2条回答 默认 最新

  • dqw70970 2014-08-28 16:25
    关注

    The major reasons why your example script would end unexpectedly or undesireably are:

    • reboot and shutdown - this should be ovious.

    • memory limits of PHP, of Apache, or running out of memory on OS level will kill this script.

    • in some setups, there might be limits for used CPU cycles, too.

    • any PHP fatal error that occurs during runtime.

    • kill <pid> with almost any signal (not only kill -9) will terminate the script.

    • If the script is running inside apache using mod_php, then apache restarts would also kill the script.
      Note that each long running script inside apache blocks a worker thread/process, which might be bad for your server performance. This may not apply in a php-fcgi environment!

    To prevent some of this:

    • Run the script outside the apache environment.
      I prefer calling echo '/usr/bin/php myscript.php'| at now. (using at scheduler, see manpage)

      at totally detaches your script from apache, so it will still run when the apache process ends.

    • set the PHP memory limit to a high value or infinite. (anything > 512M is generally a bad idea)
      This can be done inside the script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果