Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web implementation of "tail -f filename"?

Tags:

tail

I have a log file and want to create a webpage (possibly Python but not strictly) that will work much like unix "tail -f filename" command works (show new log lines when they are written to file).

So that user will continuously see log right in browser.

How would you implement this?

like image 608
Evgenyt Avatar asked Nov 02 '10 13:11

Evgenyt


People also ask

What does the command tail F filename do?

Monitoring a File for Changes The tail -f option lets you view the last ten lines of the file. This option is useful for tracking log files in real-time as this command will update when new data is added to the same file. If the original file changes, the header will display which lines were altered.

How is tail command implemented in Java?

From main method start executor service to start log file tailer, i.e. crunchifyExecutor. execute(crunchify_tailF); which internally calls run() Also call appendData() method which will add new line to file every 5 seconds. Once new line will be added to file, tailer will pick and print it to Eclipse Console.

How do you turn off tail F in Linux?

In less , you can press Ctrl-C to end forward mode and scroll through the file, then press F to go back to forward mode again. Note that less +F is advocated by many as a better alternative to tail -f .


3 Answers

I implemented this using jquery (.ajax) and php (json).

The flow is essentially as follows:

  • user calls an html page on their browser
  • html page contains an initial jquery .ajax call to a remote php script on the server that performs the required function, in this case, retrieving a few of the last lines of the file being 'tailed'
  • if no new lines are available, the php script just loops (wile the ajax caller waits, ie longpolling), and can be configured to time out if necessary (returning an appropriate value back to the ajax calling function on the client)
  • when new lines are detected by the php script, they are wrapped in a json response and sent back to the ajax calling function on the browser, which then appends it to the existing content of the page.
  • The javascript function will then recursively make the same ajax call, effectively sitting in an infinite loop.

In my specific implementation, i did the following:

  • both the ajax call on the client AND the php script on the server have timeouts to handle, for example, broken connections nicely. Also ensures the ajax call does not wait forever.
  • the ajax call passes a line number as a reference back to the server to tell it what the last line number was that it received, so the server knows which lines to return. Initially the value is zero, and the server will immediately return the last 10 lines of the file
  • when the php script is called, it uses the clients last line number to do a quick check on the file; if new lines have already been added it returns them immediately, if not it sits in a loop (1 second) and then instead checks the files ctime (or mtime) to detect when new lines are written. This is more effective than counting the lines in the file (which could be huge) every second.

See my longpolling/realtime tail implementation using jquery and php here: https://github.com/richardvk/web_file_tail

like image 156
Richard vK Avatar answered Nov 10 '22 22:11

Richard vK


Tailon is a python webapp that, among other things, provides tail -f like functionality. In addition, wtee (a sister project of tailon) can make all its stdin viewable in the browser - its use is identical to the unix tee command: tail -f filename | wtee

like image 42
gvalkov Avatar answered Nov 10 '22 22:11

gvalkov


Scullog, having capability of sharing the local drive to the browser. Stream the log file via Socket.IO over browser. It run on any platform such as windows/linux/mac. It run as service or standalone mode.

like image 38
Sanket Bajoria Avatar answered Nov 10 '22 22:11

Sanket Bajoria