Michael R
Michael R

Reputation: 259

Check a log file every 15 minutes with Python

I am trying to open a log file every 15 minutes and see if the file called a 'finalize' string. I know how to check the file for the string but I do not know if there is an easy way to have the script look every 15 minutes.

Is there a module of some sort that will allow me to do this?

[EDIT] I want this because I am writing a queue program. That is once one job calls finalize, the script will load up another job and then monitor that until it finishes, etc.

Upvotes: 1

Views: 3445

Answers (3)

abarnert
abarnert

Reputation: 365953

The sched module will let you do this. So will the Timer class in the threading module.

However, either of those is overkill for what you want.

The easy, and generally best, way to do it is to use your system's scheduler (Scheduled Tasks on Windows, Launch Services on mac, cron or a related program on most other systems) to run your program every 15 minutes.

Alternatively, just use time.sleep(15*60), and check whether time.time() is actually 15*60 seconds later than last time through the loop.


One advantage of triggering your script from outside is that it makes it very easy to change the trigger. For example, you could change it to trigger every time the file has been modified, instead of every 15 minutes, without changing your script at all. (The exact way you do that, of course, depends on your platform; LaunchServices, famd, inotify, FindFirstChangeNotification, etc. all work very differently. But every modern platform has some way to do it.)

Upvotes: 3

arkilic
arkilic

Reputation: 1

You can use sched modulescheduler.run()The function waits for the next event, then execute it and so on until there are no more scheduled events.You can create a queue of events to do this.check out the python docs

Upvotes: 0

alecxe
alecxe

Reputation: 474091

Take a look at python-tail module:

import tail

def print_line(txt):
    print 'finalize' in txt

t = tail.Tail('/var/log/syslog')
t.register_callback(print_line)
t.follow(s=60*15)

Upvotes: 2

Related Questions