Python pyinotify, pythonpyinotify

Source: Internet
Author: User

Python pyinotify, pythonpyinotify

Preface

Recently, I encountered a Python package pyinotify used to monitor log files. Based on my project experience and some online materials, I would like to summarize the principle of using the pyinotify module to monitor log folders, when the log arrives, the corresponding function is triggered for processing and the process of deleting the log file after processing is completed. The following describes pyinotify.

Pyinotify

Pyinotify is a Python module used to monitor changes in the file system. Pyinotify depends on the Linux kernel function-inotify (Kernel 2.6.13 merge ). Inotify is an event-driven notifier whose notification interface calls from the kernel space to the user space through three systems. Pyinotify combines these system calls and provides a top-level abstraction and a general method for processing these functions.

  1. Pyinotify means that the system's inotify is called to implement notifications.
  2. Inotify can monitor both files and directories.
  3. Inotify uses system calls instead of SIGIO to notify file system events.

Inotify can monitor the following file system events:

Event Name Is an Event Description
IN_ACCESS Yes File was accessed.
IN_ATTRIB Yes Metadata changed.
IN_CLOSE_NOWRITE Yes Unwrittable file was closed.
IN_CLOSE_WRITE Yes Writtable file was closed.
IN_CREATE Yes File/dir was created in watched directory.
IN_DELETE Yes File/dir was deleted in watched directory.
IN_DELETE_SELF Yes Self-deletion means that an executable file deletes itself during execution.
IN_DONT_FOLLOW No Don't follow a symlink (lk 2.6.15 ).
IN_IGNORED Yes Raised on watched item removing. Probably useless for you, prefer instead IN_DELETE *.
IN_ISDIR No Event occurred against directory. It is always piggybacked to an event. The Event structure automatically provide this information (via. is_dir)
IN_MASK_ADD No To update a mask without overwriting the previous value (lk 2.6.14). Useful when updating a watch.
IN_MODIFY Yes File was modified.
IN_MOVE_SELF Yes Self-moving, that is, an executable file moves itself during execution
IN_MOVED_FROM Yes File/dir in a watched dir was moved from X. can trace the full move of an item when IN_MOVED_TO is available too, in this case if the moved item is itself watched, its path will be updated (see IN_MOVE_SELF ).
IN_MOVED_TO Yes File/dir was moved to Y in a watched dir (see IN_MOVE_FROM ).
IN_ONLYDIR No Only watch the path if it is a directory (lk 2.6.15). Usable when calling. add_watch.
IN_OPEN Yes File was opened.
IN_Q_OVERFLOW Yes Event queued overflowed. This event doesn't belongs to any participant watch.
IN_UNMOUNT Yes The host file system is umount

IN_ACCESS: The file is accessed.

IN_MODIFY, the file is written

IN_ATTRIB: file attributes are modified, such as chmod, chown, and touch.

IN_CLOSE_WRITE, writable file closed

IN_CLOSE_NOWRITE: the file cannot be written.

IN_OPEN, the file is open

IN_MOVED_FROM: The file is removed, such as mv.

IN_MOVED_TO: The file is moved, such as mv and cp.

IN_CREATE, create a new file

IN_DELETE: The file is deleted, such as rm.

IN_DELETE_SELF: indicates that an executable file is deleted when it is executed.

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 example

#! /Usr/bin/python # coding: utf-8import osfrom pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFYclass EventHandler (ProcessEvent ): "event processing" 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 () cipher t KeyboardInterrupt: notifier. stop () breakif _ name _ = "_ main _": FSMonitor ('/root/softpython/apk_url ')

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.