Solved

Redirect PHP STDERR when used on the command line in Windows

Posted on 2012-03-23
9
631 Views
Last Modified: 2012-03-29
I'm running a PHP script from the command line, and I need to capture any error output. The command is something like this:

|php -f <phpscriptfile> 2>&1

Under Linux, this works as expected. Under Windows, it doesn't. I don't know where the error output goes, but it's not coming back to me on STDOUT. If I run this from a DOS box and specifically redirect 2> to a file, it does not go to the file, it displays on the screen.

Interestingly enough, if I misspell 'php' on the command line, the DOS error message is redirected.

Also, if I misspell the script name, that php error is redirected. It's only errors that occur during execution that don't get redirected.


php config info:

phpinfo()
PHP Version => 5.3.10

System => Windows NT ROCKET 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586
Build Date => Feb  2 2012 20:10:58
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x86
Configure Command => cscript /nologo configure.js  "--enable-snapshot-build" "--enable-debug-pack" "--disable-zts" "--disable-isapi" "--disable-nsapi" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=C:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\instantclient11\sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet" "--with-mcrypt=static" "--disable-static-analyze"
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => C:\Windows
Loaded Configuration File => (none)
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20090626
PHP Extension => 20090626
Zend Extension => 220090626
Zend Extension Build => API220090626,NTS,VC9
PHP Extension Build => API20090626,NTS,VC9
Debug Build => no
Thread Safety => disabled
Zend Memory Manager => enabled
Zend Multibyte Support => disabled
IPv6 Support => enabled
Registered PHP Streams => php, file, glob, data, http, ftp, zip, compress.zlib, phar  
Registered Stream Socket Transports => tcp, udp
Registered Stream Filters => convert.iconv.*, mcrypt.*, mdecrypt.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*

Wasn't sure if you need anything else, so I just put the first bit of phpinfo here.

Any suggestions, anyone?

Thanks.
0
Comment
Question by:JPNeron
  • 5
  • 4
9 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 37760193
does it work if you use a foo.bat with

  php -f <phpscriptfile>

and then call

  foo.bat 2>&1
0
 

Author Comment

by:JPNeron
ID: 37761253
if I do:

foo.bat 2>null

the error is still displayed in the window.

if I do:

foot.bat 1>null 2>&1

then the error goes to a file called null

If I change foo.bat to:

 php -f <phpscriptfile> 1>null2

and run it via:

foo.bat 2>&1

then output goes to null2.

I really don't want to have to create a batch file to do this, if that's at all possible.

Thanks.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37765091
I'm no M$ expert, but it sounds like you try to solve a problem where 2 highly proprietary (cmd.exe, php.exe) systems won't agree on common standards
however, php is open source, may be it can be solved there ...
sorry for no better answer
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:JPNeron
ID: 37767648
I was hoping there was a php.ini setting or something that addresses this. I can't be the first person to encounter this, can I?

Any other suggestions, anyone?

Thanks.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37767893
can you change your script? then this may be a workaround:

fclose(STDERR);
define(STDERR,fopen('php://stdout','w'));
0
 

Author Comment

by:JPNeron
ID: 37768497
It might be technically possible to change it, but it comes from a 3rd party, so when we get changes, my change will be overwritten.
0
 

Author Comment

by:JPNeron
ID: 37783315
FYI: As an experiment, I tried that change, no difference.

It doesn't seem fixable, so I've rewritten my program to call the php script in a batch file, and applied redirection on the batch file. Then I have to read the redirected STDERR file to see if anything went wrong.

Thanks anyway.
0
 

Author Closing Comment

by:JPNeron
ID: 37783326
The solution is not ideal, but seems to be the only way to do it.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37783371
good luck with cumbersome workaround for M$ which *ix fixed in early seventies last millenium ;-)
Thanks for grading.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question