?
Solved

Redirect PHP STDERR when used on the command line in Windows

Posted on 2012-03-23
9
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

718 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