Inotify安装和案例实现

Inotify介绍

inotify
Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象。

安装

rpm -qa inotify-tools #如果没安装
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install inotify-tools -y

参数详解

-e: 事件 
-d:后台运行
-m:始终保持事件监听状态
-q:打印很少的信息,仅仅打印监控事件的信息  安静状态
-r :递归查询目录
-timefmt:指定时间输出的格式
-excluder:排除文件或者目录的时候不区分大小写
Events:

        access          file or directory contents were read   访问

        modify          file or directory contents were written  修改

        attrib          file or directory attributes changed  属性发生变化

        close_write     file or directory closed, after being opened in  写入之后关闭

                        writeable mode

        close_nowrite   file or directory closed, after being opened in  

                        read-only mode

        close           file or directory closed, regardless of read/write mode 

        open            file or directory opened

        moved_to        file or directory moved to watched directory  移动到哪里

        moved_from      file or directory moved from watched directory

        move            file or directory moved to or from watched directory

        create          file or directory created within watched directory

        delete          file or directory deleted within watched directory

        delete_self     file or directory was deleted

        unmount         file system containing file or directory unmounted卸载

案例实现

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-06-05
# @Author  : ${anan} ($mail})
# @Link    : ${link}
# @Version : $v1.0$
# function :实现对当前目录的监控,若当前目录有文件内容修改,则将修改的日志打印出来。
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
try:
    while True:
        time.sleep(30)
except KeyboardInterrupt:
    observer.stop()
    observer.join()

猜你喜欢

转载自blog.csdn.net/sinat_34789167/article/details/80607288
今日推荐