Php executes shell scripts to get standard output and error output.

Source: Internet
Author: User
1. the problem is that you need to execute a shell script in the php code and want to get stdout and stderr respectively ). Using shell redirection to put the standard output and error output into the file and then using php to read the file is feasible, but the output file content is no longer needed after it is returned to php... 1. problem

Execute a shell script in the php code.RespectivelyGet itsStandard output (stdout)AndError output (stderr). Using shellRedirectionIt is feasible to put the standard output and error output into the file and then use php to read the file. but it is no longer necessary to return the content of the output file to php. Is there a way to redirect the shell output to the variable?

To execute the script function, php itself cannot be used to complete (execute the R language script), and regular output and error output must be fed back.

2. an attempt has been made.

In shellRedirectionStandard output and error output can be put into files respectively, but temporary files are generated.

my_cmd 1>out.txt 2>err.txt

Php re-readout.txtAnderr.txtFile content.

The disadvantage is that an intermediate file is generated. If the shell commandmy_cmdThe execution time is relatively long (may take several minutes), so if the same user initiates an http request multiple times within a short period of time, php will executemy_cmd, You needout.txtAnderr.txtTo avoid write conflicts. This isGenerated many unnecessary intermediate files. Even if they are deleted in a timely manner, I/O is overhead and hopefully avoided.

Phpexec()Function:

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

Parameters in$outputIt seems that only the "screen output during script running" content can be obtained, which is a mixture of stdout, stdout, and stderr, and cannot be obtained separately.

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.