tags:

views:

72

answers:

3

Hi,

I have to execute a Linux more command in PHP from a particular offset, format the result and display the result in the browser.

My code for the above is:

<html>
<head>
    <META HTTP-EQUIV=REFRESH CONTENT=10>
    <META HTTP-EQUIV=PRAGMA CONTENT=NO-CACHE>
    <title>Runtime Access log</title>
</head>
<body>
    <?php
    $moreCommand = "more +3693 /var/log/apache2/access_log | grep -v -e '.jpg' -e '.jpeg' -e '.css' -e '.js' -e '.bmp' -e '.ico'| wc -l";
     exec($moreCommand, $accessDisplay);
     echo "<br/>No of lines are : $accessDisplay[0] <br/>";
    ?>
</body>
</html>

The output at the browser is: No of lines are : 3428 (This is wrong)

While executing the same command using command line gives a different output. My code snippet for the same is:

<?php
    $moreCommand = "more +3693 /var/log/apache2/access_log | grep -v -e '.jpg' -e '.jpeg' -e '.css' -e '.js' -e '.bmp' -e '.ico'| wc -l";

    exec($moreCommand, $accessDisplay);
    echo "No of lines are : $accessDisplay[0] \n";
?>

The output at the command line is: No of lines are : 279 (This is correct)

While executing the same command directly in command line, gives me output as 279.

I am unable to understand why the output of the same command is wrong in the browser. Its actually giving the word count of lines, ignoring the offset parameter.

Thanks, Amit

A: 

What would the difference be in:

$moreCommand = "more -f -99999 +3693 /var/log/apache2/access_log | grep -v -e '.jpg' -e '.jpeg' -e '.css' -e '.js' -e '.bmp' -e '.ico' -c"

(-f & -num to more, -c to grep instead of | wc -l)

In debugging it could also be usefull to examine the exact output without count of the 2 (perhaps using head or tail), as there might be shell differences between the cli & webserver users.


OK, that was wrong, can reproduce more not getting the '+', alternative:

$moreCommand = "tail --lines=`wc -l /var/log/apache2/access_log  | awk '{print $1 - 3693}'  | grep -v -e '.jpg' -e '.jpeg' -e '.css' -e '.js' -e '.bmp' -e '.ico' `
Wrikken
Amit
Ah, now I see. I can reproduce the problem here, no idea why that would be. Added the only other solution that DOES work i could think of to the answer.
Wrikken
A: 

I think it's because of that more fails to determine screen size. There was an environment variable named LINES that holds lines of screen. I think in the first call this variable not setted, so more shows all of the lines!

Set LINES variable or use -num on your more command, hope to fix :)

Ehsan
Hi Ehsan, Thanks for your reply. I have tried with -num option but that has not helped. I have not tried setting the lines variable. The problem I am working on, does not have a fix line number to be displayed. I have to display the access log of apache, but from an offset set(+linenum in more command) to the present cursor position. I am unable to get that. Is there any other way to do it ?
Amit
+1  A: 

I would recommend that you drop the shell pipeline and parse the logfile in PHP directly. Much more control. Much less hassle and definitely more robust.

Noufal Ibrahim
Hi Noufal, thanx for your reply. The problem I have to work on is, I have to display the access log, but from an offset(counter of the last displayed line), to the present cursor position. How can I achieve that in php? Can you please help me with the sample code snippet ? Thanx
Amit
You could just read line by line upto your desired offset skipping all the lines till then and then start processing. I'm not sure what you mean by "cursor position" since this is non interactive.
Noufal Ibrahim