Home  >  Article  >  Backend Development  >  Redirect - php executes the shell script and gets the standard output and error output

Redirect - php executes the shell script and gets the standard output and error output

WBOY
WBOYOriginal
2016-09-08 08:43:571928browse

1. Question

You need to execute a shell script in php code, and hope to get its standard output (stdout) and error output (stderr) respectively. Using shell's redirect to put the standard output and error output into a file and then using php to read the file is feasible, but the content of the output file is no longer needed after it is transferred back to php. Is there a way to redirect the shell's output to variable?

To execute the function of the script, it cannot be completed with PHP itself (execute the script in R language), and both regular output and error output need to be fed back.

2.Already tried

Redirection

in shell can put the standard output and error output into files respectively, but generate temporary files.

<code class="bash">my_cmd 1>out.txt 2>err.txt</code>
php then reads the contents of the

out.txt and err.txt files and returns them.

Disadvantages of this: intermediate files are generated. If the shell command

my_cmd takes a long time to execute (maybe several minutes), then if the same user initiates multiple http requests in a short period of time to cause php to execute my_cmd, out.txt and err will be required each time. The names of txt are all different to avoid writing conflicts. This produces a lot of unnecessary intermediate files. Even if they are deleted in time, there will still be I/O overhead, which I hope can be avoided.

php’s

exec() function:

<code>string exec ( string $command [, array &$output [, int &$return_var ]] )</code>
The parameters inside $output

seem to only get the content of "screen output when running the script", which is stdout or a mixture of stdout and stderr and cannot be obtained separately.

Reply content:

1. Question

You need to execute a shell script in the php code, and hope

to get its

standard output (stdout) and error output (stderr) respectively. Using the shell's redirect to put the standard output and error output into a file and then using php to read the file is feasible, but the content of the output file is no longer needed after it is transferred back to php. Is there a way to redirect the shell's output to variable? To execute the function of the script, it cannot be completed with PHP itself (execute the script in R language), and both regular output and error output need to be fed back.

2.Already tried

Redirection

in

shell can put the standard output and error output into files respectively, but generate temporary files.

<code class="bash">my_cmd 1>out.txt 2>err.txt</code>
php then reads the contents of the out.txt and

err.txt files and returns them. Disadvantages of this: intermediate files are generated. If the shell command my_cmd

takes a long time to execute (maybe several minutes), then if the same user initiates multiple http requests in a short period of time to cause php to execute

my_cmd, out.txt and err will be required each time. The names of txt are all different to avoid writing conflicts. This produces a lot of unnecessary intermediate files. Even if they are deleted in time, there will still be I/O overhead, which I hope can be avoided. php’s exec()

function:

<code>string exec ( string $command [, array &$output [, int &$return_var ]] )</code>
The parameters inside $output seem to only get the content of "screen output when running the script", which is stdout or a mixture of stdout and stderr and cannot be obtained separately.

I found the answer, ask and answer it yourself. Use the

proc_open()

function, which can execute a shell script and save stdout and stderr separately. It also supports setting stdin:

<code>resource proc_open ( string $cmd , array $descriptorspec , array &$pipes [, string $cwd [, array $env [, array $other_options ]]] )</code>

Reference code: <pre class="brush:php;toolbar:false">&lt;code class=&quot;php&quot;&gt;$descriptorspec = array( 0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;), // stdin 1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;), // stdout 2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;) // stderr ); $cmd = 'Rscript hello.r'; // 替换为你要执行的shell脚本 $proc = proc_open($cmd, $descriptorspec, $pipes, null, null); // $proc为false,表明命令执行失败 if ($proc == false) { // do sth with HTTP response } else { $stdout = stream_get_contents($pipes[1]); fclose($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); $status = proc_close($proc); // 释放proc } $data = array( 'code' =&gt; 0, 'msg' =&gt; array( 'stdout' =&gt; $stdout, 'stderr' =&gt; $stderr, 'retval' =&gt; $status ) ); // do sth with $data to HTTP response&lt;/code&gt;</pre>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn