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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值