I have again another trouble with using exec in php
my os is suse linux and I am using php 5.1.2Somehow my output is being trimmed when i use exec()
in linux
~ -> ps -ef | grep java
root 3548 1 0 Aug05 ? 00:00:01 /usr/java/jdk1.5.0_13//bin/java -server -Djava.awt.headless=true -Xms512m -Xmx512m -XX:NewSize=224m -XX:MaxNewSize=256m -XX:SurvivorRatio=8 -XX:+UseParallelGC -jar /jfe-server.jar start
psinl 14811 1 0 09:12 ? 00:00:01 /usr/java/jdk1.5.0_13//bin/java -server -Djava.awt.headless=true -Xms512m -Xmx512m -XX:NewSize=224m -XX:MaxNewSize=256m -XX:Survi开发者_Go百科vorRatio=8 -XX:+UseParallelGC -jar jfe-server.jar start
psinl 18164 18080 0 16:20 pts/1 00:00:00 grep java
but when output to web via
<div>Checking whether JFEServer has been started</div>
<div><pre><?php exec('ps -ef | grep java',$output,$result);
print_r($output); ?></pre>
</div>
</br>
And my output on the web
Checking whether JFEServer has been started
Array
(
[0] => root 3548 1 0 Aug05 ? 00:00:01 /usr/java/jdk1.5.0_13//bin/java
[1] => psinl 14811 1 0 09:13 ? 00:00:01 /usr/java/jdk1.5.0_13//bin/java
[2] => psinl 18069 14271 0 16:20 ? 00:00:00 sh -c ps -ef | grep java
[3] => psinl 18071 18069 0 16:20 ? 00:00:00 grep java
)
Why is that php has automatically trimmed off my output even I didnt want it to?
This is because PHP just doesn't can't allocate a large enough buffer for you to use with exec. Your best bet is to add a step in between: pipe the output to a temporary file in your exec()
call
example: exec('ps -ef | grep java > /tmp/mytmpfilename.txt')
...then dump that out to the screen with a call to file_get_contents()
example: var_dump(file_get_contents('/tmp/mytmpfilename.txt'));
edit: Alternatively, you can use file() if there is a LOT data to output, like several thousand lines worth.
You could use passthru, which passes the output of a command directly to the clients browser.
<div>Checking whether JFEServer has been started</div>
<div><pre><?php passthru( 'ps -ef | grep java', $result ); ?></pre></div>
<br />
If that doesn't help, you should look into the documentation of ps, if it tests the standard output terminal type (e.g. file/pipe/terminal). If it does so, it could be trimming it to some default width if it can't determine the actual terminal width. On my debian based server it does. The correct command on my machine is:
<div>Checking whether JFEServer has been started</div>
<div><pre><?php passthru( 'ps -efww | grep java', $result ); ?></pre></div>
<br />
I had to add the -w
flag to double my ps output on Centos 6.2. Of course, this only is necessary sometimes, specifically in a session run from a script.
In a TTY session, ps will not trim the output, but in other circumstances (depending on the TERM
variable, it will. You can also explicitly set it to unlimited by adding -ww
.
ps
man pages were the key for me on this one.
php did not trim you output, the browser did. check the original output by Right click -> View Page Source on browser.
精彩评论