Home>

What is inotify:

inotify is an event-driven notification mechanism,inotify provides a simple api using the smallest file descriptor,And allows fine-grained monitoring.Communication with inotify is achieved through system calls.The available functions are as follows: inotify_init is a system call used to create an inotify instance,And returns a file descriptor pointing to the instance. inotify_init1 is similar to inotify_init with additional flags.If these additional flags are not specified,Will take the same value as inotify_init. inotify_add_watch increase monitoring of files or directories,And specify which events need to be monitored.Flags are used to control whether events are added to existing monitoring,Whether to monitor only paths that represent a directory,Whether to follow symbolic links,Whether to perform one-time monitoring,Stop monitoring when the first incident occurs. inotify_rm_watch Remove watch items from watch list. read Reads a buffer containing one or more event messages. close closes the file descriptor,And remove all monitoring on this descriptor.When all file descriptors for an instance are closed,Both resources and underlying objects will be released,For the kernel to use again.

Therefore, a typical monitoring program needs to do the following:

Use inotify_init to open a file descriptor Add one or more monitors Waiting for event Handle events,Then return and wait for more events When monitoring is no longer active,Or after receiving a signal,Close the file descriptor,Empty and exit.

Installation of pyinotify package

git clone https://github.com/seb-m/pyinotify.git
cd pyinotify /
python setup.py install

File system events that inotify can monitor include:

in_access, ie the file is accessed

in_modify, file is written

in_attrib, file attributes are modified,Such as chmod, chown, touch, etc.

in_close_write, writable file is closed

in_close_nowrite, non-writable file is closed

in_open, the file is open

in_moved_from, the file was removed,Like mv

in_moved_to, the file was moved,Such as mv, cp

in_create, create new file

in_delete, the file is deleted,Such as rm

in_delete_self, self-delete, that is, an executable deletes itself during execution

in_move_self, self-moving, that is, an executable file moves itself during execution

in_unmount, the host file system is umount

in_close, the file is closed,Equivalent to (in_close_write | in_close_nowrite)

in_move, the file is moved,Equivalent to (in_moved_from | in_moved_to)

pyinotify usage example

#!/usr/bin/env python
#encoding:utf-8
import os
from pyinotify import watchmanager, notifier, \
processevent, in_delete, in_create, in_modify
class eventhandler (processevent):
 "" "Event Handling" ""
 def process_in_create (self, event):
  print "create file:%s"%os.path.join (event.path, event.name)
 def process_in_delete (self, event):
  print "delete file:%s"%os.path.join (event.path, event.name)
 def process_in_modify (self, event):
   print "modify file:%s"%os.path.join (event.path, event.name)
def fsmonitor (path="."):
  wm=watchmanager ()
  mask=in_delete | in_create | in_modify
  notifier=notifier (wm, eventhandler ())
  wm.add_watch (path, mask, auto_add=true, rec=true)
  print "now starting monitor%s"%(path)
  while true:
   try:
     notifier.process_events ()
     if notifier.check_events ():
       notifier.read_events ()
   except keyboardinterrupt:
     notifier.stop ()
     break
if __name__ == "__main__":
 fsmonitor ("/home/firefoxbug")
  • Previous js onmousewheel event is triggered multiple times
  • Next Summary of multi-threaded experience using Net