0

I have a log file assume it to be any text(txt) file.

I am reading it using php and performing functions.

The log file gets updated mostly every 10 seconds by a program as a normal log file does though the time interval is not fixed. I am ready to take some delay in showing the results.

One method is using cron jobs (which looks quite and odd to refresh the script every 10-20 seconds).

Assume the log file and the php file on the same server

I have my own dedicated server Ubuntu

Can anyone tell me a method through which i can read the file ?

Something like the php file gets executed whenever the file changes or do i have to use python/java or some other language for it ??

If the answer still sticks to cronjobs how do i add them in my Ubuntu server(i have php as a apache module) ?

Thanks

2
  • While you provide some details, your question is very unspecific. How do you want to display the log file? On a web page? What does the "parsing" entail, or do you just want to show it? Otherwise write a simple script that uses readfile() Commented Jun 22, 2011 at 13:34
  • @mario well i can use the readfile() but my question is how do i refresh the php file Commented Jun 22, 2011 at 13:37

2 Answers 2

1
$fp = fopen('/path/to/log/file', 'r');
while (true) {
    $line = fgets($fp);
    if ($line === false) {
        echo "no new content, sleeping\n";
        sleep(3);
        fseek($fp, 0, SEEK_CUR);
    } else {
        echo $line;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way that i can execute this file using SSH or do i have to open it in the browser ? :D I mean i have a cui webserver how can i open that file in it ? Rest the answer looks great ? Thanks
If you have the CLI version of PHP installed on your server, yes, just run "/path/to/php -f /path/to/script" or similar. Note this will never exit -- the while(true) statement makes it loop forever.
0

You can use:

# tail -f log.txt

or whatever the log file is named on the command line. This will keep the last few lines displayed in the terminal. You can also specify how many lines to display, like

# tail -n 100 -f log.txt

5 Comments

how does php come in to play ? and what does tail does ?
i mean how will php refresh itself ?
PHP writes to the log files right? When pages are accessed? Are you just trying to monitor the log file or are you trying to display the log file to a PHP page?
So that being the case, all you need to do is keep a terminal window open (or ssh into the server), and use the tail command to display the last few lines of the log file. As it changes, you'll see the changes displayed in the terminal. Is this what you are trying to do?
ha no . I got what i wanted that could ofc help somewhere else :D Thanks for trying :D :D

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.