Search code examples
phptail

PHP Tail - Getting the last 10 lines only


I have to make a script that tails the last 10 lines of a screenlog.0 file in Ubuntu. The problem is that no matter what I try doing myself I end up failing. However, when I was searching for an alternative scripted by someone else, a more experienced developer, I found this: https://gist.github.com/karabanov/3818740

It is a nice piece of code that doesn't use much memory. My problem is that I don't know how to make it retrieve only the last 10 lines of the file. It retrieves all of them and that's something I really want to avoid.

Could you explain my what I need to modify in the code in order to make it work how I need it to work? Or maybe you have better scripts. Any help is appreciated.

P.S: I am a newbie PHP developer and I started working with PHP few months ago. I need this script in order to display the console for each game server in my game panel project.


Solution

  • using that library is an overkill, the simple tail command will work fine in ubuntu

    tail -n 10 /you/file/full/path/here
    

    if you want to get this from within a php script you can use

    $string = exec( 'tail -n 10 /you/file/full/path/here');