1

I'm trying to monitor a log file with tail -f , parse it to extract some data with grep and pass the data as a mysql query. I could do that by passing each new line detected by tailf to a php script, but I don't know how to do that.. Or i could simulate tailf with php directly, but how to I monitor a file for changes with php? I think just by having a while , look for the size , remember the last position, seek it and read till the difference right? Anyone can give some hints on what's better to use? or simpler? Also i've heard named pipes could be a solution but don't know how to grab the data from there The logger is nginx by the way.. thank you

PartySoft
  • 2,749
  • 7
  • 39
  • 55

2 Answers2

3

In shell you can do something like:

tail -f file.log|grep whatever-you-want|while read line; do 
  echo $line
done

Just change the line echo $line to what you want - you can call PHP or whatever you want in that line, $line is the the line from tail/grep combination.

Take a look at these SOq for more info:

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
0

you are mixing to different approaches. tail -f means you will have a process running all the time and if the log file is truncated (rotated) you will most probably get stuck. If you need good thing for log monitoring (i assume linux) then you have to monitor the file for changes and seek to the remembered position. The IMHO best way is to rely on an existing solution. syslog-ng seems to come firs to to mind. It can pipe your log files into your program. Though not the case with non sysloged logs. The I would opt for http://www.php.net/manual/en/intro.inotify.php. This will require a running process. As the final (the order depends on your situation) solution I would go for a periodic checking with cron.

Michael Tabolsky
  • 3,429
  • 2
  • 18
  • 11
  • what I've experienced with PHP is that it tends to block the file when it reads it (of course I will open just for reading non blocking) – PartySoft Dec 01 '10 at 09:35
  • hopefully you don't plan to code something that will block. if you plan to use such approach for an app that has to manage concurrency of access where you can get dozens if not hundreds of processes blocked in IO wait to read a file .... you'd better think again what you are doing :) – Michael Tabolsky Dec 01 '10 at 18:56