Redirect-PHP executes shell scripts to get standard output and error output

Source: Internet
Author: User

1. Questions

In PHP code, you need to execute a shell script to get its standard output (stdout) and error Output (stderr) separately . Using shell redirection to put the standard output and error output into a file and then read the file in PHP, it is possible, but the output file content passed back to PHP is no longer necessary, there is no way to redirect the shell output to the variable?

The ability to execute scripts cannot be done with PHP itself (scripts that execute the R language), and you need to give feedback on both the normal output and the error output.

2. An attempt has been made

The redirect inside the shell can place the standard output and the error output in a file, but generates a temporary file.

my_cmd 1>out.txt 2>err.txt

PHP re out.txt -Read and err.txt file contents are returned.

Disadvantages of doing so: intermediate files are produced. If the shell command my_cmd executes for a long time (which may take a few minutes), then the same user who initiates HTTP requests multiple times within a short period of time will need to have my_cmd out.txt a different name each time to err.txt avoid writing conflicts. This creates a lot of unnecessary intermediate files . Even if you delete them in a timely manner, there is a cost to I/O and I hope to avoid them.

PHP's exec() functions:

string exec ( string $command [, array &$output [, int &$return_var ]] )

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

Reply content:

1. Questions

In PHP code, you need to execute a shell script to get its standard output (stdout) and error Output (stderr) separately . Using shell redirection to put the standard output and error output into a file and then read the file in PHP, it is possible, but the output file content passed back to PHP is no longer necessary, there is no way to redirect the shell output to the variable?

The ability to execute scripts cannot be done with PHP itself (scripts that execute the R language), and you need to give feedback on both the normal output and the error output.

2. An attempt has been made

The redirect inside the shell can place the standard output and the error output in a file, but generates a temporary file.

my_cmd 1>out.txt 2>err.txt

PHP re out.txt -Read and err.txt file contents are returned.

Disadvantages of doing so: intermediate files are produced. If the shell command my_cmd executes for a long time (which may take a few minutes), then the same user who initiates HTTP requests multiple times within a short period of time will need to have my_cmd out.txt a different name each time to err.txt avoid writing conflicts. This creates a lot of unnecessary intermediate files . Even if you delete them in a timely manner, there is a cost to I/O and I hope to avoid them.

PHP's exec() functions:

string exec ( string $command [, array &$output [, int &$return_var ]] )

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

Find the answer, ask yourself to answer it once.
Using proc_open() a function, it can execute a shell script and save stdout and stderr separately, and also supports setting stdin:

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

Reference code:

$descriptorspec = array(        0 => array("pipe", "r"),    // stdin        1 => array("pipe", "w"),    // stdout        2 => array("pipe", "w")     // 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' => 0,        'msg' => array(            'stdout' => $stdout,            'stderr' => $stderr,            'retval' => $status        )    );        // do sth with $data to HTTP response
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.