PHP shell_exec (), exec () and system () return only partial output

I am trying to use a PHP script to run the siege command and capture the output.

Performing the following in the shell gives the following results:

$ /usr/local/bin/siege -c30 -t30s -f urls.txt ..... HTTP/1.1 200 0.10 secs: 11246 bytes ==> GET /*******.html HTTP/1.1 200 0.11 secs: 11169 bytes ==> GET /*******.html HTTP/1.1 200 0.10 secs: 11246 bytes ==> GET /*******.html Lifting the server siege.. done. Transactions: 1479 hits Availability: 100.00 % Elapsed time: 29.05 secs Data transferred: 14.69 MB Response time: 0.10 secs Transaction rate: 50.91 trans/sec Throughput: 0.51 MB/sec Concurrency: 5.33 Successful transactions: 1479 Failed transactions: 0 Longest transaction: 0.16 Shortest transaction: 0.09 

When executing the same command in PHP via exec (), shell_exec (), system (), I get only the following output.

 HTTP/1.1 200 0.10 secs: 11246 bytes ==> GET /*******.html HTTP/1.1 200 0.11 secs: 11169 bytes ==> GET /*******.html HTTP/1.1 200 0.10 secs: 11246 bytes ==> GET /*******.html 

Since I am really only interested in the results of the siege, this data is useless to me. For some reason, he ignores the results of the siege.

Here is an example of what I'm doing in PHP ...

 exec('/usr/local/bin/siege -c30 -t30s -f urls.txt', $output); 
+6
source share
1 answer

The siege program writes its output to two different standard streams : stdout and stderr . PHP exec() captures only stdout . To capture both, you need to redirect (using your shell) stderr to stdout so that everything is on the same thread that PHP captures.

To do this, add 2>&1 to the very end of your command. In your example, this would be:

 exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output); 

(I set up a siege and verified that it uses both stdout and stderr and that output redirection works.)

+4
source

Source: https://habr.com/ru/post/951948/


All Articles