[Tutor] Reset while loop

David david at abbottdavid.com
Wed May 6 22:28:30 CEST 2009


Alan Gauld wrote:
> 
> "David" <david at abbottdavid.com> wrote
> 
> 
>> I was playing around with trying to create a python program like tail 
>> -f as discussed on the list. i came up with this;

Alan, thanks for your time and great tips :)
> 
> import os
> 
> def get_tail(fname, bufsize, linesep):
>         f = open(fname, 'rb')
>         f.seek(0, 2)
>         pos, tailsize = divmod(f.tell(), bufsize)
>         if tailsize == 0:
>             pos = max(0, pos-1)
>         pos *= bufsize
>         f.seek(pos)
>         lines = f.read().split(linesep)
>         f.close()
>         return lines[-2:]
> 
> 
> def tail_it(fname, bufsize=8192, linesep=os.linesep):
>     while True:
>         new_time = os.stat(tail_fname).st_mtime
>         if new_time > old_time:
>             time.sleep(sec_to_wait)
>             print get_tail (fname, bufsize, linesep)
>         old_time = new_time
> 
> I haven't tried it but that should be close to what you want.
> 
> 
This helps a lot because I can now see how the loop works. One of my 
problems is putting the pieces of code in the right places. It helps me 
understand when I can run the program and see the errors and output to 
make adjustments.

> OTOH since you only use the last two lines why not miss all
> that out and go straight to where you want to be with:
> 
> f.seek(-buffsize, os.SEEK_END)    # goto one buffsize away from end
> return f.read().split(linesep)[-2:]
I like it!

Here is what I came up with;

#!/usr/bin/python

"Python program similar to tail -f"
import os
import sys
import commands
import time

sec_to_wait = 60
fname = raw_input('Enter the log file to be monitored: ')

def usage():
     """Explains the program to the user"""
	print \
"You will need to be root to monitor most log files."

def check_whoami():
     """Check if Root"""
     if commands.getoutput('whoami') != 'root':
         sys.exit('\nYou will need to be Root!\n')

def print_log_end(fname, bufsize, linesep):
     f = open(fname, 'r')
     f.seek(-bufsize, os.SEEK_END)    # goto one buffsize away from end
     log = f.read().split(linesep)[-8:]
     for i in log:
	print i
     f.close()

def tail_it(fname, bufsize, linesep):
     old_time = os.stat(fname).st_mtime
     while True:
	new_time = os.stat(fname).st_mtime
	if new_time > old_time:
	    time.sleep(sec_to_wait)
	    updated = print_tail(fname, bufsize, linesep)
	    for i in updated:
		print i
	old_time = new_time
				
def print_tail(fname, bufsize, linesep):
     f = open(fname, 'r')
     f.seek(-bufsize, os.SEEK_END)    # goto one buffsize away from end
     return f.read().split(linesep)[-2:]
     f.close()

if __name__ == "__main__":
     print_log_end(fname, bufsize=8192, linesep=os.linesep)
     check_whoami()
     tail_it(fname, bufsize=8192, linesep=os.linesep)

[output]
Enter the log file to be monitored: /var/log/messages
May  6 16:00:01 opteron cron[24679]: (root) CMD (rm -f 
/var/spool/cron/lastrun/cron.hourly)
May  6 16:00:01 opteron run-crons[24685]: (root) CMD 
(/etc/cron.hourly/moodle)
May  6 16:00:01 opteron run-crons[24688]: (root) CMD 
(/etc/cron.hourly/vnstat)
May  6 16:03:06 opteron su[24712]: Successful su for root by david
May  6 16:03:06 opteron su[24712]: + pts/6 david:root
May  6 16:03:06 opteron su[24712]: pam_unix(su:session): session opened 
for user root by david(uid=1000)
May  6 16:10:01 opteron cron[24751]: (root) CMD (test -x 
/usr/sbin/run-crons && /usr/sbin/run-crons )

May  6 16:20:01 opteron cron[24788]: (root) CMD (test -x 
/usr/sbin/run-crons && /usr/sbin/run-crons )


-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list