0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
0

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' `

2 Comments

Hi Wrikken, thanks for your reply. Here my intention is not to count the lines but to get the lines itself. The command in my program is : $moreCommand = "more +$existingWordCount /var/log/apache2/access_log | grep -v -e '.jpg' -e '.jpeg' -e '.css' -e '.js' -e '.bmp' -e '.ico'". I have used "wc -l" just for ease of highlighting my problem and to show the difference b/w my command line o/p and browser o/p I have tried using -f & -num option but it has not helped to serve the purpose. The php script still ignores the +linenum parameter. Is there any other option available ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.