解决 php-fpm php://stdout 命令行 打印 失效问题

转载请注明出处,有疑问或错误请发邮件到 xiaozhi@fslib.org
 


<!DOCTYPE html>
<html>
<body>

<h1>我的第一张 PHP 页面</h1>

<?php
echo "Hello World!";
$fd = fopen('php://stdout', 'w');
    if ($fd) {
        fwrite($fd, "7777888");
        fwrite($fd, "\n");
        fclose($fd);
    }
?>

</body>
</html>

对以上代码,如果用php-cgi运行,在命令行(windows和linux)都会获得如下结果

7777888

而对于生产环境的php-fpm是关闭了stdout的,源码如下

int fpm_stdio_init_main() /* {{{ */
{
 int fd = open("/dev/null", O_RDWR);

 if (0 > fd) {
  zlog(ZLOG_SYSERROR, "failed to init stdio: open(\"/dev/null\")");
  return -1;
 }

 if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
  zlog(ZLOG_SYSERROR, "failed to init stdio: dup2()");
  close(fd);
  return -1;
 }
 close(fd);
 return 0;
}

 

经研究,在linux下可让程序显示加载不同的动态库,从而可以在不修改php源码的基础上解决此问题,配上管道捕捉效果如下

Please slecet a registered process:
1:[2015-11-06 12:27:39.966]fsServer:366(127.0.0.1)
2:[2015-11-06 12:27:40.916]php-fpm:405(127.0.0.1)
3:[2015-11-06 12:27:41.817]sos:367(127.0.0.1)
use name or index:2
100316/1323588888(doRecvDataFunction_011)--Select is:"php-fpm:405(127.0.0.1)".
Start debug stream:"php-fpm:405(127.0.0.1)".
7777888
7777888
7777888
7777888
7777888
7777888

 

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值