<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]协程 shell_exec 如何捕获标准错误流]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Swoole专题研究]]></category>
<pubDate>Tue, 03 Nov 2020 06:30:48 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	问题：用Co&#92;System::exec()执行了一个不存在的命令时，错误信息会直接打印到屏幕，而不是返回错误信息。<br/>怎么办：实际上Swoole提供的System::exec()行为上与PHP的shell_exec是完全一致的，我们写一个shell_exec的同步阻塞版本，执行后发现同样拿不到标准错误流输出的内容，会被直接打印到屏幕。<br/>答案：&nbsp;&nbsp;使用proc_open+hook实现。<br/>现象：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$result = shell_exec(&#039;unknown&#039;);
var_dump($result);
</textarea><br/><br/>==================================================================<br/>#php tt.php<br/>PHP Warning:&nbsp;&nbsp;shell_exec() has been disabled for security reasons in /data/www/testswoole/tt.php on line 2<br/>NULL<br/><br/>估计整成错误输出了，于是用：<br/>php tt.php &gt;/dev/null 2&gt;&amp;1<br/><br/>果然，怎么办？加入管道，将此错误指向新的文件描述符。<br/><br/><br/><br/>前置：打开php.ini里面的proc_open禁用函数。<br/>#cat t.php <br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
Swoole&#92;Runtime::setHookFlags(SWOOLE_HOOK_ALL);
Swoole&#92;Coroutine&#92;run(function () &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$descriptorspec = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;),
&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;&nbsp;&nbsp;$process = proc_open(&#039;unknown&#039;, $descriptorspec, $pipes);
&nbsp;&nbsp;&nbsp;&nbsp;var_dump($pipes);

&nbsp;&nbsp;&nbsp;&nbsp;var_dump(fread($pipes[2], 8192));
&nbsp;&nbsp;&nbsp;&nbsp;$return_value = proc_close($process);

&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;command returned $return_value&#92;n&quot;;
&#125;);

</textarea><br/><br/>#php t.php <br/>array(3) &#123;<br/>&nbsp;&nbsp;[0]=&gt;<br/>&nbsp;&nbsp;resource(4) of type (stream)<br/>&nbsp;&nbsp;[1]=&gt;<br/>&nbsp;&nbsp;resource(5) of type (stream)<br/>&nbsp;&nbsp;[2]=&gt;<br/>&nbsp;&nbsp;resource(6) of type (stream)<br/>&#125;<br/>string(31) &quot;sh: unknown: command not found<br/>&quot;<br/>command returned 32512<br/><br/>代码来自rango：https://mp.weixin.qq.com/s/z7SPy-tV3hsQqLXZ-tKlZw
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]协程 shell_exec 如何捕获标准错误流]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>