I have a log file that is constantly growing. How can I watch and parse it via a Ruby script?
The script will parse each new line as it is written to the file and output something to the screen when the new line contains the string 'ERROR'
I have a log file that is constantly growing. How can I watch and parse it via a Ruby script?
The script will parse each new line as it is written to the file and output something to the screen when the new line contains the string 'ERROR'
check out file-tail gem
Working on idea of @Qianjigui but not using 100% CPU:
You can use
Kernel#select
in the following way:Then call it like:
I've elided all error checking and such - you will want to have that and probably some mechanism to break out of the loop. But the basic idea is there.
Poor man's approach for quick stuff:
a Ruby script that does
Running screen with:
Thanks for the ezpz's idea, using the select method you get get what you want. The select method is listening the IO's stream, read the bytes what comes 'late'.
There are two approach:
sleep
inside the infinite loop)Here is an article I wrote about this: Ruby for Admins: Reading Growing Files. So the program combining both event subsystem and polling looks like this: