中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

用pyinotify監(jiān)控文件系統(tǒng)示例

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
    Pyinotify是一個(gè)Python模塊,用來監(jiān)測(cè)文件系統(tǒng)的變化。 Pyinotify依賴于Linux內(nèi)核的功能—inotify(內(nèi)核2.6.13合并)。 inotify的是一個(gè)事件驅(qū)動(dòng)的通知器,其通知接口通過三個(gè)系統(tǒng)調(diào)用從內(nèi)核空間到用戶空間。pyinotify結(jié)合這些系統(tǒng)調(diào)用,并提供一個(gè)頂級(jí)的抽象和一個(gè)通用的方式來處理這些功能。

pyinotify其實(shí)就是通過調(diào)用系統(tǒng)的inotify來實(shí)現(xiàn)通知的。

 

1. 安裝

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

2. 簡(jiǎn)單使用

import os
from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY

class EventHandler(ProcessEvent):
    def process_IN_CREATE(self, event):
        print "Create file:%s." %os.path.join(event.path,event.name)

        os.system('cp -rf %s /tmp/bak/'%(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():
                print "check event true."
                notifier.read_events()
        except KeyboardInterrupt:
            print "keyboard Interrupt."
            notifier.stop()
            break

if __name__ == "__main__":
    FsMonitor("/root/work/")


標(biāo)簽: linux

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP實(shí)現(xiàn)導(dǎo)出Excel文件通用方法

下一篇:PHP實(shí)現(xiàn)zip壓縮解壓通用函數(shù)