【问题标题】:Apache kills long time running processApache杀死长时间运行的进程
【发布时间】:2014-10-01 15:16:20
【问题描述】:

在 linux apache 服务器上(ubuntu 14.04 lts,apache 2.4.7 with mpm_prefork 和 mod_php)我有 PHP 脚本需要很长时间。这些都被 apache 杀死了。

我们调整了 php 设置 (max_execution_time, set_time_limit...)

我们在日志中没有任何痕迹(syslog、apache 访问/错误日志)

我们已经用 strace 追踪了 apache 进程:

2172 is the script process
1939 is the apache main process
....

2172  14:53:01 +++ killed by SIGKILL +++
1939  14:53:01 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED, si_pid=2172, si_status=SIGKILL, si_utime=3067, si_stime=38} ---

【问题讨论】:

  • 我建议长时间运行的 php 脚本应该在后台运行(而不是在网络服务器中),并且应该在数据库中“留下线索”以了解进度的“状态”。这可以在网络浏览器中以“用户友好”的方式报告。

标签: php apache ubuntu-14.04 sigkill mod-php


【解决方案1】:

尝试设置ini_set('max_execution_time', -1); 或者以root身份运行这个脚本,那么apache不会杀死他

【讨论】:

  • 我们尝试了 max_execution_time -1 但结果相同。并在 bash 上以 root 身份运行脚本......脚本没有被杀死。
  • 20-30 分钟取决于处理 Apache 随机杀死脚本。 2 分钟、10 分钟、15 分钟......在少数情况下脚本不会被杀死
  • 试试:header('Content-Type: text/html; charset=utf-8'); ini_set('max_execution_time', -1); ini_set('memory_limit', '512M');
  • KEEP ALIVE 已开启?在配置中?
  • yes : KeepAlive On MaxKeepAliveRequests 500 (test with 100) KeepAliveTimeout 3 (test with 5)
【解决方案2】:

另一种可能性是 Apache2 正在杀死该进程,因为它在一定时间内没有发回任何东西。这通常发生在共享主机上。如果您正在使用输出缓冲,请将其关闭。然后每隔一段时间打印出一些东西并立即使用flush() 将信息发送回Apache。

例如;在最长的循环中,您可以执行以下操作:

$time = time();
while($looping) {
  ... Code here ...
  if(time() > $time) {
    echo '.';
    flush();
    //ob_flush();//If you're using output buffering (often on by default)
    $time = time();
  }
}

【讨论】:

  • 我们在 while 中尝试这个:echo "
    ";回声 memory_get_usage();冲洗(); ob_flush(); $cpt=0;
  • 应该可以。还可以尝试使用set_time_limit(30),这会将脚本运行的时间增加 30 秒
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多