Solved

Redirect PHP STDERR when used on the command line in Windows

Posted on 2012-03-23
9
630 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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