Using Perl File::Tail in php code
Moderator: General Moderators
Using Perl File::Tail in php code
Hi all,
I'm a novice when it comes to working in both php and perl so I was wondering what would be the best way to go about this?
I'm trying to include Perl's File::Tail within some php code that I'm currently working on. The code looks as follows:
#!/usr/bin/perl -w
BEGIN { unshift @INC,"./blib/lib/";}
use File::Tail 0.8;
$name="/home/WorkSpace/logfile" unless $name=shift @ARGV;
$debug=shift @ARGV || 0;
print "Looking at $name \n";
$file=File::Tail->new(name=>$name,debug=>$debug,interval=>1,maxinterval=>5,
adjustafter=>10,resetafter=>500,errmode=>"return") or
die "Could not open $name: $!";
while ($line=$file->read)
{print "$line";}
It's a pretty dry cut/paste of one of the existing test files but I want to work with the $line thats returned in my php code.
So any thoughts on the best way to go about this? Thanks.
I'm a novice when it comes to working in both php and perl so I was wondering what would be the best way to go about this?
I'm trying to include Perl's File::Tail within some php code that I'm currently working on. The code looks as follows:
#!/usr/bin/perl -w
BEGIN { unshift @INC,"./blib/lib/";}
use File::Tail 0.8;
$name="/home/WorkSpace/logfile" unless $name=shift @ARGV;
$debug=shift @ARGV || 0;
print "Looking at $name \n";
$file=File::Tail->new(name=>$name,debug=>$debug,interval=>1,maxinterval=>5,
adjustafter=>10,resetafter=>500,errmode=>"return") or
die "Could not open $name: $!";
while ($line=$file->read)
{print "$line";}
It's a pretty dry cut/paste of one of the existing test files but I want to work with the $line thats returned in my php code.
So any thoughts on the best way to go about this? Thanks.
Re: Using Perl File::Tail in php code
I'm using File::Tail to monitor a log file so are you saying I could use exec or system to run the Perl file and return it's output back to within the php code?
Re: Using Perl File::Tail in php code
Actually, I take what I said back. You'd best do all the coding in PHP.
The Perl code will never stop, so exec/system will never return.
Try a combination of fopen, fgets, and stream_set_blocking.
Admittedly, not as flexible as the Perl you posted, but works well nonetheless.
The Perl code will never stop, so exec/system will never return.
Try a combination of fopen, fgets, and stream_set_blocking.
Code: Select all
$h = fopen("/home/WorkSpace/logfile", "r");
stream_set_blocking($h, true);
for (;true;) echo fgets($h);Re: Using Perl File::Tail in php code
This works but the fgets outputs everything in the file and I would just need the last line for what I'm trying to do. Is there a way to have to ignore everything else and just send me back the last line of the log file?
And I know I haven't said it so thanks for the help.
And I know I haven't said it so thanks for the help.
Re: Using Perl File::Tail in php code
Code: Select all
$contents = file("path/to/log.txt");
echo array_pop($contents);Re: Using Perl File::Tail in php code
Code: Select all
$handle = popen("tac " . $filename,'r');
$line = fgets($handle);Re: Using Perl File::Tail in php code
Argh, yet another hitch. I've combine the codes above to get this:
$filename = "/home/WorkSpace/logfile";
$h = popen("tac " . $filename, "r");
stream_set_blocking($h, true);
$line = fgets($h);
echo $line;
Now when I run this code in the terminal "php tail_file.php," I receive the last line in the log file. However, when I include this into my index.php for my Apache homepage, the last line is not echoed on the web-browser. How come and what can I do to fix it? Thanks again.
$filename = "/home/WorkSpace/logfile";
$h = popen("tac " . $filename, "r");
stream_set_blocking($h, true);
$line = fgets($h);
echo $line;
Now when I run this code in the terminal "php tail_file.php," I receive the last line in the log file. However, when I include this into my index.php for my Apache homepage, the last line is not echoed on the web-browser. How come and what can I do to fix it? Thanks again.
Re: Using Perl File::Tail in php code
Dump everything that can go wrong:
Of course this too verbose, but you get the picture. Post here the results of your dumps.
Code: Select all
$filename = "/home/WorkSpace/logfile";
echo 'File is readable: ';
var_dump(is_readable($filename));
$h = popen("tac " . $filename, "r");
echo '<br />Handle is a pointer: ';
var_dump($h);
stream_set_blocking($h, true);
$line = fgets($h);
//echo $line;
echo '<br />Line is: ';
var_dump($line);
Re: Using Perl File::Tail in php code
File is readable: bool(false)
Handle is a pointer: resource(2) of type (stream)
Line is: bool(false)
However when I run it in the terminal, "php lastlog.php" I get the following:
File is readable: bool(true)
<br />Handle is a pointer: resource(4) of type (stream)
<br />Line is: string(63) "The line I want (text)
"
Handle is a pointer: resource(2) of type (stream)
Line is: bool(false)
However when I run it in the terminal, "php lastlog.php" I get the following:
File is readable: bool(true)
<br />Handle is a pointer: resource(4) of type (stream)
<br />Line is: string(63) "The line I want (text)
"
Last edited by Artex7 on Sun Nov 16, 2008 10:58 pm, edited 1 time in total.
Re: Using Perl File::Tail in php code
File is not readable. Is the path absolute? does it have the right permissions?
Remember that PHP running in the command line is running under the user you are logged in as, possibly root. In the browser PHP runs as apache, so the file should be accessible by apache as well.
Remember that PHP running in the command line is running under the user you are logged in as, possibly root. In the browser PHP runs as apache, so the file should be accessible by apache as well.
Re: Using Perl File::Tail in php code
Troubleshooting it: I chmod 777 the file just to make sure it had all permissions but it still couldn't read it. Next I moved the file to under "/" and it was able to read it then. So I'm guessing there is something wrong with my path to the file? The path I have is actually "/home/<myusername>/WorkSpace/logfile" so does it have something to do with being under my username?
Re: Using Perl File::Tail in php code
Permissions are not enough, check the owner of the file / directory (run ls -la). It is probably root root, and you need to chown it to apache (chown = change owner) for apache to be able to read it.