LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   PHP shell_exec() always gives error "Broken pipe" when using pipe (https://www.linuxquestions.org/questions/linux-server-73/php-shell_exec-always-gives-error-broken-pipe-when-using-pipe-924436/)

burek 01-18-2012 10:24 AM

PHP shell_exec() always gives error "Broken pipe" when using pipe
 
Hi all.

I have an error im my apache's error.log saying:
Code:

/bin/grep: writing output: Broken pipe
I've traced the source of the problem to this line:
Code:

shell_exec("/bin/netstat -e -p --tcp -n 2> /dev/null | /bin/grep -v root | /bin/grep -v 127.0.0.1 | /bin/grep -cE '.*(python|transmissionc|wget|nzbperl|java).*'");

I replaced (just for test) the upper line with:
Code:

shell_exec("ls | ls | ls | ls");
and now I get:
Code:

ls: write error: Broken pipe
My conclusion was that something is wrong with PHP handling pipes or some linux's security feature is preventing php script to execute that pipe, because when I try the same from shell, I don't get such errors.
I've tried with su -c "ls | ls | ls | ls" www-data and also with su - www-data followed with ls | ls | ls | ls without errors.

I also get the same error if I make a test.php file containing just:
Code:

<?
shell_exec('ls | ls | ls | ls');

and running it with php -f test.php 2>&1, gave me the same error:
Code:

# php -f test.php 2>&1
ls: write error: Broken pipe

(I even tried with just ls | ls and it gave out the same error but not always (1 out of 6 tries).

Can you please just shoot out any idea that first gets to your mind of what the problem could actually be?

I have torrentflux-b4rt installed on my debian machine (stable, squeeze 6.0.3, "Linux ninja 2.6.26-2-amd64 #1 SMP Wed Sep 21 03:36:44 UTC 2011 x86_64 GNU/Linux") with apache web server (apache2 2.2.16-6+squeeze4) and php5 (libapache2-mod-php5 5.3.3-7+squeeze3) installed.

I'm not sure if I've chosen the correct forum, but I hope mods will point this out to me if I'm wrong.

burek 01-19-2012 06:04 AM

I've solved this issue. It had to do something with ulimit command.
Even though I had uncommented line "session required pam_limits.so" in /etc/pam.d/login and I had put
Code:

* hard nofile 102400
* soft nofile 102400
* hard nproc  102400
* soft nproc  102400

www-data soft nproc 10240
www-data hard nproc 40960

in /etc/security/limits.conf
it still didn't want to create more than 20 apache processes (required by torrentflux)
I've restarted apache2 numerous times and finally I managed to solve this by adding:
Code:

root soft nproc 102400
root hard nproc 102400

in /etc/security/limits.conf
and also changing apache config file /etc/apache2/apache2.conf by modifying this section:
Code:

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers      5
    MaxSpareServers    100
    MaxClients          150
    MaxRequestsPerChild  0
</IfModule>

( MaxSpareServers was 10 and I've changed it to 100 )
After the restart of apache with /etc/init.d/apache2 restart, everything worked great (so far) :)
Hope this helps someone in the future :)


All times are GMT -5. The time now is 08:16 PM.