|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-26 21:15 UTC] poehler at interworx dot com
Description: ------------ See reproduce code and expected / actual result sections for details. Short story is when exec'ing with a pipe to 'head', we're getting unexpected results. I'm aware this may be 'correct' behavior, by some definition, but for the life of me I can't figure out where this behavior is documented, if that is the case. So if that is the case, please point me in the right direction, if you could be so kind. Reproduce code: --------------- test.sh: ================= #!/bin/sh for i in 1 2 3 4 5 do echo "Welcome $i times" sleep 1 done ================== test.php: ================== <? passthru( '/bin/sh test.sh | head -n1' ); ================== Expected result: ---------------- Here is the output of running the same command at the shell, which is correct. ================== [root@me]# time /bin/sh test.sh | head -n1 Welcome 1 times real 0m1.030s user 0m0.002s sys 0m0.027s ================== And I would expect identical (save minor timing differences) when test.php is run. Actual result: -------------- [root@me]# php test.php Welcome 1 times test.sh: line 5: echo: write error: Broken pipe test.sh: line 5: echo: write error: Broken pipe test.sh: line 5: echo: write error: Broken pipe test.sh: line 5: echo: write error: Broken pipe real 0m5.191s user 0m0.002s sys 0m0.038s Note that this took 5 seconds instead of 1. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 28 15:00:02 2025 UTC |
I also have this issue. I've tried to test a simple example on several of our web hosting machines and all of them have this issue present. Simple test. Create a php file (test.php) with this content: ================== <?php shell_exec('ls | ls | ls | ls' ); ================== and then run: php -f test.php 2>&1 you should get the error like: ls: write error: Broken pipe almost every time you run that command. I'm not sure what's the reason for this, but I'm pretty sure it's the bug, since the bash/sh doesn't ever raise that error no matter how many ls'es we put into the pipe.Just to say, my problem somehow got misteriosly solved. I don't know exactly what was the culprit, but I know I've messed around with ulimit and with apache2.conf: <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 100 MaxClients 150 MaxRequestsPerChild 0 </IfModule> More details here (if needed): http://www.linuxquestions.org/questions/showthread.php?p=4577927Ok, so I finally think to have cracked the problem: This seem to be the equivalent of exec('cmd', &$output) that does not create a broken pipe: $p = proc_open("ls | ls | ls | ls | ls | ls | ls | echo `date` >> foobar | ls", array( array("pipe","r"), array("pipe","w"), array("pipe","w") ), $pipes ); while (($buffer = fgets($pipes[1])) !== false) $output[] = trim($buffer); foreach ($pipes as $pipe) fclose($pipe); if (isset($output)) print_r($output); There is no broken pipe and the file foobar now contains the output of date.