I am writing a python program to monitor a file for any modification to the file by an external program. I used the below sample program for monitoring the TEST.txt file but the sample program only work for directory and not for the file. Any help appreciated to make it working for file.
import time
import fcntl
import os
import signal
FNAME = "/HOME/PRASAD/TEST.txt"
def handler(signum, frame):
print "File %s modified" % (FNAME,)
signal.signal(signal.SIGIO, handler)
fd = os.open(FNAME, os.O_RDONLY)
fcntl.fcntl(fd, fcntl.F_SETSIG, 0)
fcntl.fcntl(fd, fcntl.F_NOTIFY,
fcntl.DN_MODIFY | fcntl.DN_CREATE | fcntl.DN_MULTISHOT)
while True:
time.sleep(10000)
inotifycommand viasubprocess, but that's also likely worse on memory