Page 1 of 1

Monitor a text file realtime

Posted: Thu Jun 01, 2006 7:55 am
by jp_loh
Is there a way for me to monitor a file realtime in PHP from the CLI? I'm trying to copy a PERL script to PHP since I can't do PERL.

I want to upload data from a log file to the database as soon as an entry pops out.

I'm starting from this command:

Code: Select all

tail -n 0 -f <file> | php <script>
I'm not sure if this will put the entries in stdin or somewhere else like an argument.

Thanks!

Posted: Thu Jun 01, 2006 9:05 am
by ok
I don't get it... why you use CLI???

Posted: Thu Jun 01, 2006 9:11 am
by Weirdan
ok wrote:I don't get it... why you use CLI???
Why not? What's wrong with using CLI?

Posted: Thu Jun 01, 2006 9:14 am
by Weirdan
jp_loh, you could try something like this:

Code: Select all

set_time_limit(0);
$fp = fopen('php://stdin', 'r');
while($line = fgets($fp)) {
   // do something to $line
}
fclose($fp);

Posted: Thu Jun 01, 2006 10:13 am
by jp_loh
Weirdan wrote:jp_loh, you could try something like this:

Code: Select all

set_time_limit(0);
$fp = fopen('php://stdin', 'r');
while($line = fgets($fp)) {
   // do something to $line
}
fclose($fp);
Hey thanks! I'll try that.

Posted: Tue Jul 04, 2006 1:23 am
by jp_loh
Okay, I'm back. I just realized that if I put a call to tail in rc.local, it waits for tail to end.

The problem is that I have three lines that will call tail in rc.local. So what happens is the first call is only executed. Is there another way to monitor a log file realtime (still using CLI)?