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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器