Welcome, guest | Sign In | My Account | Store | Cart

Sometimes tail -f launched for log file miss the point when program recreates log file. Script in this recipe monitors inode changes for specified file and restarts tail if needed.

Python, 74 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# -*- coding: Windows-1251 -*-
'''
File: tail-f.py

Call 'tail -f' for specified file and restarts when file's inode changed
'''
import os
import subprocess
import time

__author__ = 'Denis Barmenkov <denis.barmenkov@gmail.com>'
__source__ = 'http://code.activestate.com/recipes/577398/'

SLEEP_TIME = 1.0 # seconds
DELIM_LINE = '=' * 65
TAIL_PID = None

def get_inode(filename):
    '''get inode for file'''
    if not os.path.exists(filename):
        return None
    else:
        return os.stat(filename)[1]

def kill_tail(pid):
    '''kill tail on specified PID'''
    if pid:
        os.system('kill -9 %d' % pid)

def run_tail(filename):
    '''run tail -f on file, returns tails's PID'''
    if not os.path.exists(filename):
        return None

    process = subprocess.Popen(['tail', '-f', filename], bufsize=1)
    return process.pid

def monitor_file(filename):
    '''main monitor function'''
    global TAIL_PID
    inode = None

    while 1:
        inode_new = get_inode(filename)
        if inode_new != inode:
            kill_tail(TAIL_PID)
            print DELIM_LINE
            print 'restarted: tail -f %s' % filename
            print DELIM_LINE
            TAIL_PID = run_tail(filename)
            inode = inode_new
        else:
            time.sleep(SLEEP_TIME)

def break_handler(signum, frame):
    '''basic interrupt handling'''
    kill_tail(TAIL_PID)
    print
    print DELIM_LINE
    print 'Interrupted by signal %r' % signum
    sys.exit(0)    

if __name__ == '__main__':
    import sys
    import signal

    for sig_name in 'SIGABRT SIGBREAK SIGILL SIGINT SIGTERM'.split():
        if not hasattr(signal, sig_name):
            continue
        sig_id = getattr(signal, sig_name)
        signal.signal(sig_id, break_handler)

    monitor_file(sys.argv[1])

1 comment

Jason 13 years ago  # | flag

tail -F can also do this