tail -f for windows


Here’s a simple implementation of “tail -f” UNIX command equivalent in python. I used it a lot on windows servers to monitor log files. The good thing is that it works over SMB (windows shares).

#!/usr/bin/env python
import sys, os, time

def main(argv):
    if len(argv) < 2:
        print "Usage: tail filename.log"
        exit(1)
    try:
        fp = open(argv[1], "r")
        st_results = os.stat(argv[1])
        st_size = st_results[6]
        fp.seek(st_size)

        while 1:
            where = fp.tell()
            line = fp.readline()
            if not line:
                time.sleep(1)
                fp.seek(where)
            else:
                print line,
                fp.close()
            except:
                fp.close()

                if __name__=="__main__":
                    main(sys.argv)

This is useful when starting/stopping remote services with Windows Service Controller:

sc \\server1 stop "Macromedia JRun4 default Server"
sc \\server1 start "Macromedia JRun4 default Server"