|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
How to monitor a file for changes?
I am trying to write a script that will monitor a file for changes, and then send an email with the changes. I am unsure on how to proceed. Here is some meta-code of what I am trying to do:
Code:
while 1:
#watch for changes in file
#if change, read line of file, send email
reading the file and sending an email is easy, I am just not sure how I would watch for the file to be changed. |
|
#2
|
||||
|
||||
|
Check the file's date/time perhaps?
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#3
|
|||
|
|||
|
You want the stat() command in the os module:
Quote:
My guess is that you probably want one of those. |
|
#4
|
||||
|
||||
|
I have to agree with scorp, you'll need to keep an eye on the last modifide time for the file, you can get this using the os.path.getmtime() function.. i'd sugest you take a look at the os.path module for more details.
If your on *ix you could use cron to run the script every 5 min or so, if not your gonna have to loop, it may not be the most eligant way to do things but it work's ![]() You can then use the smtplib module to email the file as an attachment (or in the emails body) to wherever you need to. Pretty cool If you need any more help i'd be happy to lend a hand ![]() Have fun, Mark. |
|
#5
|
||||
|
||||
|
Thanks, stat() was all I need to get going. Right now the program sets itself up as a daemon, then I have a loop that keeps the script going. I use time.sleep(5) so it checks the file's modification time every 5 seconds.
I'll put a link to the first version of a completed script when I am done, if anyone is interested. |
|
#6
|
||||
|
||||
|
You got my interest! Let us know when you done
or if you need anything else..Mark. |
|
#7
|
||||
|
||||
|
I'd be interested, too, so feel free to post it as soon as you're done.
|
|
#8
|
|||
|
|||
|
This script, by A.M. Kuchling, might be of interest.
|
|
#9
|
||||
|
||||
|
1st draft
Here is my first version of the log watching script. Right now, it is set up to monitor my php error log and email me on php parse or fatal error. It isn't very configurable yet, but if anyone has ideas for making it better, send them my way. This is mainly an excersise for learning python syntax, so I'm sure the script could be improved.
Code:
#!/usr/local/bin/python2.2
def daemonize():
""" Become a daemon"""
import os,sys
if os.fork(): os._exit(0)
os.setsid()
sys.stdin = sys.__stdin__ = open('/dev/null','r')
sys.stdout = sys.__stdout__ = open('/dev/null','w')
sys.stdout = sys.__stderr__ = os.dup(sys.stdout.fileno())
def daemonize_log_watcher():
import time, os, re, smtplib
""" Configs. Should probably move these to a config file? or take args? """
log_filename = '/var/log/php_errors.log'
search_keywords = ['PHP Parse error', 'PHP Fatal error']
mailserver = 'localhost'
From = 'py_log_watch@example.com'
To = 'email@example.com'
Subj = 'Log Watcher Alert'
""" Open File, get last mod time """
file = open(log_filename, 'r')
watcher = os.stat(log_filename)
this_modified = last_modified = watcher.st_mtime
""" Go to the end of the file """
file.seek(0,2)
""" Main Loop """
while 1:
if this_modified > last_modified:
last_modified = this_modified
""" File was modified, so read new lines, look for error keywords """
while 1:
line = file.readline()
if not line: break
for keyword in search_keywords:
if re.search(keyword, line):
text = ('From: %s\nTo: %s\nSubject: %s\n' % (From, To, Subj)) + line
server = smtplib.SMTP(mailserver)
failed = server.sendmail(From, To, text)
server.quit()
watcher = os.stat(log_filename)
this_modified = watcher.st_mtime
time.sleep(1)
if __name__=='__main__':
daemonize()
daemonize_log_watcher()
Last edited by Stew_McGruff : September 23rd, 2003 at 05:05 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to monitor a file for changes? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|