Juste comme ça, pour s'amuser.
Voici un script Python 'pytail.py' qui fait exactement la même chose que la commande tail -f /path/to/log/file sous Linux, sauf que ce script peut-être utilisé sur tous les systèmes où la commande tail -f fait défaut (comme sous Windows par exemple).
Testé avec Python 3
#!/usr/bin/env python3
import re, os, time, sys
from time import time, sleep
from os.path import join, getsize, getmtime
if len(sys.argv) == 1:
print("Argument Missing !")
print("Usage:\n {0:s} /path/to/log/file".format(sys.argv[0]))
sys.exit(1)
startTime = time()
mTimeStart = 0
fileName = sys.argv[1]
if not os.path.exists(fileName) or not os.path.isfile(fileName):
print("This file '{0:s}' does not exist !".format(fileName))
sys.exit(2)
try:
with open(fileName, 'r') as f:
f.read(1)
except Exception as e:
print("This file '{0:s}' cannot be opened for reading !".format(fileName))
sys.exit(3)
folderLogs = os.path.dirname(fileName)
copyLines = []
try:
while True:
with open(fileName, 'r') as f:
lines = f.readlines()
show = False
if len(copyLines) == 0:
showLines = lines[-10:]
show = True
elif len(copyLines) != len(lines):
showLines = lines[len(copyLines) - len(lines):]
show = True
if show:
copyLines = lines.copy()
for line in showLines:
print(line, end='')
sleep(1)
except KeyboardInterrupt as e:
print("Program stopped by user !")
sys.exit(0)
except Exception as e:
print("Unknown error during execution !")
print(e)
sys.exit(4)
Pour l'exécuter:
# python3 pytail.py /var/log/mail.log
Merci qui, merci Python.