-
-
Save mackee/3224458 to your computer and use it in GitHub Desktop.
日付などでファイルが切り替わるログをtailで表示します.新しいファイルができると,そちらに切り替わります.引数でログファイルが置かれるディレクトリを指定してください.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
import time | |
import subprocess | |
from threading import Thread | |
import signal | |
def getPath(dir, prefix = None): | |
files = sorted(filter(lambda f: os.path.isfile(dir + f), os.listdir(dir))) | |
if None != prefix: | |
files = filter(lambda f: f.startswith(prefix), files) | |
return dir + files[-1] | |
class Tailer: | |
proc = None | |
def __call__(self, path): | |
self.proc = subprocess.Popen('tail -f ' + path, shell=True) | |
argv = sys.argv | |
if 1 == len(argv): | |
print(u'第一引数に監視対象のディレクトリを指定してください') | |
print(u'第二引数にファイルの接頭語を指定することが任意でできます') | |
sys.exit() | |
if '/' == argv[1][-1]: | |
dir = argv[1] | |
else: | |
dir = argv[1] + '/' | |
if 3 != len(argv): | |
prefix = None | |
else: | |
prefix = argv[2] | |
print('watching: ' + dir) | |
last = '' | |
tailer = None | |
while(True): | |
path = getPath(dir, prefix) | |
if path != last: | |
print path | |
if None != tailer: | |
os.kill(tailer.proc.pid, signal.SIGUSR1) | |
last = path | |
tailer = Tailer() | |
thread = Thread(target=tailer, args=(path, )) | |
thread.start() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment