Solved

Redirect PHP STDERR when used on the command line in Windows

Posted on 2012-03-23
9
624 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:JPNeron
Comment Utility
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
Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
can you change your script? then this may be a workaround:

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

Author Comment

by:JPNeron
Comment Utility
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
Comment Utility
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
Comment Utility
The solution is not ideal, but seems to be the only way to do it.
0
 
LVL 51

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now