How to capture and display output from a task via Windows CMD

I have a PHP script that I run from the command line (windows) that does a lot of tasks, and the only output it gives is "print" statements that are printed directly to the screen.

I want to do this in the log file too.

I know I can:

php-cli script.php > log.txt  

      

But the problem with this approach is that all the output is being written to the log file, but I can't see how things work on average (so I can stop the process if something is mocking).

To preempt other possible questions, I cannot change the entire print to the log statement as there are too many of them and I don't want to change anything in the code without being accused of something going FUBAR. Plus there is a lack of time. I also have to run this on a windows machine.

Thank you in advance:)

Edit: Thanks for the answers guys, in the end I went with the browser because it was the easiest and fastest to set up, although I'm convinced there is a real answer to this problem somewhere.

+1


source to share


4 answers


I have always opened the log file in my web browser. This allows me to easily update it and not interrupt the writing to the file that Windows does. It's not particularly elegant, but it really works!



+1


source


You can create a powershell script file that runs a command, reads data from the STDOUT command, and then writes the output to a log file and terminal for viewing. You can use the Write-Output and Write-Host commands.

Microsoft website: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/tee-object.mspx



Another option would be to use find tee program which will read input and redirect it to two different outputs. I believe I've seen them for windows, but I don't think they are standard.

Wikipedia: http://en.wikipedia.org/wiki/Tee_(command)

+3


source


You need the "tee" command for Windows. See http://en.wikipedia.org/wiki/Tee_(command)

Powershell includes the tee command, and there are also numerous tee versions available for Windows, for example:

Can also be implemented in VBScript if you prefer.

EDIT : It just occurred to me to note the tail command: http://en.wikipedia.org/wiki/Tail_(Unix) . The tail allows you to read the last N lines of a file, and also includes a "file monitor" mode that constantly displays the end of the file in real time. This is ideal for monitoring log files because it allows you to view the log in real time without interfering with the logging process. There are several implementations of tail for Windows, both command line and GUI. Microsoft Services For UNIX packages (or what they call now) also includes a tail version. Some examples:

Some of them go well beyond simply displaying a file in real time as it updates and can send email alerts and colorize line matches, track multiple files at once, and more.

+1


source


Slow:

for /f "delims=" %a in ('php-cli script.php') do @echo %a&echo %a>>log.txt

      

or in a batch file:

for /f "delims=" %%a in ('php-cli script.php') do @echo %%a&echo %%a>>log.txt

      

0


source







All Articles