anush schreef:
On Oct 14, 10:16 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>anushschreef:
>>I have a a few log files in a directory. Each of these log files have
status messages of the system.
Eg:
2007-06-07 14:30 Critical
2007-06-07 14:40 Error
What I need to do is list all the files in the directory which have
critical messages in the recent 10 lines. So I have to read 10 lines
of each of the file, look for the first instance of 'Critical' in it
and list is the file name there. Can anybody here give me some
pointers on how to go about it.
thanks,
Anush
Hi,
If you are on *nix with PHP, have a look at a command named: tail
It is NOT a PHP command but a UNIX command, so you'll need exec,
passthru, etc to pull that trick.http://nl3.php.net/manual/en/ref.exec.php
tail -n 10 filename
http://en.wikipedia.org/wiki/Tail_(Unix)
If that is not possible, I think you must do it yourself in PHP, eg load
the whole file using something like:
$lines = file("path/to/your/logfile");
And then take the last 10 elements in that array.http://nl3.php.net/manual/en/function.file.php
Good luck.
Regards,
Erwin Moller
--
Thanks Erwin. I tried that. I Tail-ed it and then piped it to the
Grep. WOrks but I am receving only the output of 2 files in the PHP
code while it works perfectly in the shell.
$cmd=shell_exec("tail -n 10 ".$logf2." | grep -m 1 Critical ");
-
Anush
Hi Anush,
WHat do you mean that you receive only output of 2 files?
Do you mean that the file your variable $logf2 points to can only open
some files (2 in this case), and a few others not?
If so, I think you do not have readrights on the other files.
try this:
if (!is_readable($logf2)){
echo "Cannot read $logf2.";
exit;
}
$cmd=shell_exec("tail -n 10 ".$logf2." | grep -m 1 Critical ");
If that says it cannot read it, then you know where the problem lies:
PHP cannot open the file.
Remember that PHP runs seldom as user PHP, but most often as user that
Apache runs as (often www-data, or apache, or nobody).
You can test this easily by letting PHP create a file (in a directory
that is wide open, eg writing rights for everybody), and then check who
owns the file.
Regards,
Erwin Moller
--