Python Inotify monitoring LINUX file system event monitoring Python Inotify LINUX file system events

Inotify LINUX file system events that can be monitored include:

 --IN_ACCESS, ie the file is accessed 
--IN_MODIFY, the file is the Write 
--IN_ATTRIB, file properties are modified, such as chmod, chown, touch and other 
--IN_CLOSE_WRITE, the file is writable use Close 
--IN_CLOSE_NOWRITE, the file is not writable use Close 
- -IN_OPEN, the file is Open 
--IN_MOVED_FROM, the file is removed, such as mv 
--IN_MOVED_TO, files are moved to, such as mv, cp 
--IN_CREATE, create a new file 
--IN_DELETE, the file is deleted, such as RM 
--IN_DELETE_SELF , delete from, i.e. an executable file delete itself when executed 
--IN_MOVE_SELF, from the mobile, i.e. an executable file when performing move their 
--IN_UNMOUNT, host file system is umount 
--IN_CLOSE, the file is closed, equivalent to (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) 
--IN_MOVE, files are moved, equivalent to (IN_MOVED_FROM | IN_MOVED_TO) 

github Home: https://github.com/seb-m/pyinotify/wiki/Events-types

Installation pyinotify:

# pip install pyinotify

It will install the version available from the default warehouse, if you want the latest stable version, you can press down as follows clone from git repository:

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

How to use pyinotify in Linux

Copy the code
#coding:utf8
#author:lcamry

import os
import pyinotify
from functions import *
 
WATCH_PATH = '' # monitor directory
 
if not WATCH_PATH:
  wlog('Error',"The WATCH_PATH setting MUST be set.")
  sys.exit()
else:
  if os.path.exists(WATCH_PATH):
    wlog('Watch status','Found watch path: path=%s.' % (WATCH_PATH))
  else:
    wlog('Error','The watch path NOT exists, watching stop now: path=%s.' % (WATCH_PATH))
    sys.exit()
 
class OnIOHandler(pyinotify.ProcessEvent):
  def process_IN_CREATE(self, event):
    wlog('Action',"create file: %s " % os.path.join(event.path,event.name))
 
  def process_IN_DELETE(self, event):
    wlog('Action',"delete file: %s " % os.path.join(event.path,event.name))
 
  def process_IN_MODIFY(self, event):
    wlog('Action',"modify file: %s " % os.path.join(event.path,event.name))
 
def auto_compile(path = '.'):
  wm = pyinotify.WatchManager()
  mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
  notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())
  notifier.start()
  wm.add_watch(path, mask,rec = True,auto_add = True)
  wlog('Start Watch','Start monitoring %s' % path)
  while True:
    try:
      notifier.process_events()
      if notifier.check_events():
        notifier.read_events()
    except KeyboardInterrupt:
      notifier.stop()
      break
 
if __name__ == "__main__":
   auto_compile(WATCH_PATH)
Copy the code

For more examples, see: https: //www.cnblogs.com/hollyspirit/p/3182828.html

In the following example, the root user monitor I (ssh to) tecmint user's home directory (/ home / tecmint) change under, as shown in screenshot:

# python -m pyinotify -v /home/tecmint

接下来,我会观察到任何 web 目录 (/var/www/html/) 的更改:

# python -m pyinotify -v /var/www/html/tecmint.com

要退出程序,只要按下 Ctrl+C。

注意:当你在运行 pyinotify 时如果没有指定要监视的目录,/tmp 将作为默认目录。

Inotify 可以监视的LINUX文件系统事件包括:

 --IN_ACCESS,即文件被访问 
--IN_MODIFY,文件被write 
--IN_ATTRIB,文件属性被修改,如chmod、chown、touch等 
--IN_CLOSE_WRITE,可写文件被close 
--IN_CLOSE_NOWRITE,不可写文件被close 
--IN_OPEN,文件被open 
--IN_MOVED_FROM,文件被移走,如mv 
--IN_MOVED_TO,文件被移来,如mv、cp 
--IN_CREATE,创建新文件 
--IN_DELETE,文件被删除,如rm 
--IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己 
--IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己 
--IN_UNMOUNT,宿主文件系统被umount 
--IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) 
--IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO) 

github主页:https://github.com/seb-m/pyinotify/wiki/Events-types

安装 pyinotify:

# pip install pyinotify

它会从默认仓库安装可用的版本,如果你想要最新的稳定版,可以按如下从 git 仓库 clone 下来:

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

如何在 Linux 中使用 pyinotify

Copy the code
#coding:utf8
#author:lcamry

import os
import pyinotify
from functions import *
 
WATCH_PATH = '' #监控目录
 
if not WATCH_PATH:
  wlog('Error',"The WATCH_PATH setting MUST be set.")
  sys.exit()
else:
  if os.path.exists(WATCH_PATH):
    wlog('Watch status','Found watch path: path=%s.' % (WATCH_PATH))
  else:
    wlog('Error','The watch path NOT exists, watching stop now: path=%s.' % (WATCH_PATH))
    sys.exit()
 
class OnIOHandler(pyinotify.ProcessEvent):
  def process_IN_CREATE(self, event):
    wlog('Action',"create file: %s " % os.path.join(event.path,event.name))
 
  def process_IN_DELETE(self, event):
    wlog('Action',"delete file: %s " % os.path.join(event.path,event.name))
 
  def process_IN_MODIFY(self, event):
    wlog('Action',"modify file: %s " % os.path.join(event.path,event.name))
 
def auto_compile(path = '.'):
  wm = pyinotify.WatchManager()
  mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
  notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())
  notifier.start()
  wm.add_watch(path, mask,rec = True,auto_add = True)
  wlog('Start Watch','Start monitoring %s' % path)
  while True:
    try:
      notifier.process_events()
      if notifier.check_events():
        notifier.read_events()
    except KeyboardInterrupt:
      notifier.stop()
      break
 
if __name__ == "__main__":
   auto_compile(WATCH_PATH)
Copy the code

For more examples, see: https: //www.cnblogs.com/hollyspirit/p/3182828.html

In the following example, the root user monitor I (ssh to) tecmint user's home directory (/ home / tecmint) change under, as shown in screenshot:

# python -m pyinotify -v /home/tecmint

Next, I would observe any web directory (/ var / the WWW / HTML / ) changes:

# python -m pyinotify -v /var/www/html/tecmint.com

To exit the program, just press Ctrl + C.

Note: When you run pyinotify If you do not specify a directory to be monitored, / tmp is the default directory.

Guess you like

Origin www.cnblogs.com/qiumingcheng/p/12209156.html