Updated 2012-09-24 09:42:41 by LkpPo

Comp.lang.tcl questioners frequently demand some sort of gadget to monitor-an-ongoing-process-and-display-the-result-in-a-text. Tailing widget is the simplest answer CL knows that meets the common intent of these requests.

There are lots of variations on this theme, though. tailf carefully implements the functionality of the Unix tail(1), and adds a bit of filtering capability. I've written a few examples [1] that show how different combinations of IPC, fileevent, after, ... can co-operate to monitor continuing processes.

A utility to filter the output of tail is greptail. It lazily uses the unix tail(1) program.

Flog (Follow LOG) outputs updates to the newest of a sequence of files whose names match a given prefix.

Also see directory notification package in Tcl (Linux 2.4 and higher).

And a more generic approach is outlined on the file and directory change notifications page.

Here is another version. It continuously opens and closes the file and seems to work where a normal "tail -f" fails. This is the case at my internet provider's ssh-account (Linux infong159 2.4.24-grsec-20040109a). Issuing a "tail -f access_log" on the shell there will produce no output when I access the homepage, but using this little proc, I get instant response (well, within a second at least). I don't think this is a logfile-rotation issue, though:
 proc tail-f {filename} {
    # get an initial last position:
    set fh [open $filename r]
    seek $fh 0 end
    set pos [tell $fh]
    close $fh
    # ask again all second:
    while 1 {
         after 1000
         set fh [open $filename r]
         fconfigure $fh -blocking no -buffering line
         seek $fh $pos start
         while {[eof $fh] == 0} {
             gets $fh line
             if {[string length $line] > 0} {puts $line}
        }
        set pos [tell $fh]
        close $fh
    }
 }

A better approach using the tcl-inotify package can be found in tailf-inotify. It relies on a compiled extension and a particular feature in the Linux kernel however, so it is not a universal solution.