Hey all,

I want to design a php script that will watch log files and post there changes in real time.

I know the linux command is tail -f /var/log/your log you wanna watch. But my question is... will php hang here until the command completes (which it never will).

is there a better way of doing this other then a page refresh?

perhaps java?

    AJAX + [man]shell_exec[/man]('tail --lines=30 /path/to/log/fil')

      i assume that AJAX is an apache plugin?

      would you have a code example? for im sure it's not as simple as just calling the shell_exec command in an applet.

        AJAX is a commonly used technique whereby JavaScript is used to call server-side scripts (e.g.: PHP), receive output from those scripts, and alter the contents of the web page based on that output. Search on something like "AJAX and PHP" and you should receive more info than you'll know what to do with. 😉

          i did a google search on it and it came up with some examples of how to code without using the httpxml request.

          however.. what do i need server side! I figure i need to get server side up and running before i can even think about coding it.

            <?php
            // output last 30 lines of log file:
            header('Content-Type: text/plain');
            echo shell_exec('tail --lines=30 /path/to/log/file');
            

            This assumes your host config allows the shell_exec() function, the webserver user under whom PHP scripts execute has read permission on the log file, and my recollection of how to use the tail command is correct.

              that works.. and i found a ajax learning program on w3 schools.

              However.. I'm looking to get real time results.

              the problem here is that every time ajax gets the file.. it will repost the last 30 lines regardless of update or not.

                So what would you expect to see when there is an update? Flashing lights, the newest lines in red, or what? I'm not trying to be facetious -- OK, maybe a little -- but from a user's standpoint, what's the difference between having the same lines redisplayed every n seconds until the file is appended, at which time the oldest line will be removed, everything else will move up, and the latest line will appear at the bottom of the list?

                If you need a clearer indication that things have changed, then you could use [man]filemtime/man to record the last modification time of the log file (in a DB or something?), and each time the script is called check to see if it has changed. If so, tail it and output the result, or whatever you want to do to draw attention to the fact that it has changed.

                  Write a Reply...