Monitor a text file realtime

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jp_loh
Forum Newbie
Posts: 3
Joined: Thu Jun 01, 2006 7:47 am
Location: Philippines
Contact:

Monitor a text file realtime

Post 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!
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I don't get it... why you use CLI???
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

ok wrote:I don't get it... why you use CLI???
Why not? What's wrong with using CLI?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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);
jp_loh
Forum Newbie
Posts: 3
Joined: Thu Jun 01, 2006 7:47 am
Location: Philippines
Contact:

Post 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.
jp_loh
Forum Newbie
Posts: 3
Joined: Thu Jun 01, 2006 7:47 am
Location: Philippines
Contact:

Post 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)?
Post Reply