|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!Pyinotify是一个Python模块,用来监测文件体系的变更。Pyinotify依附于Linux内核的功效—inotify(内核2.6.13兼并)。inotify的是一个事务驱动的关照器,其关照接口经由过程三个体系挪用从内核空间到用户空间。pyinotify分离这些体系挪用,并供应一个***的笼统和一个通用的体例来处置这些功效。
- pyinotify说百了就是经由过程挪用体系的inotify来完成关照的
- inotify既能够监督文件,也能够监督目次
- Inotify利用体系挪用而非SIGIO来关照文件体系事务。
Inotify能够监督的文件体系事务包含:
EventNameIsanEventDescriptionIN_ACCESSYesfilewasaccessed.IN_ATTRIBYesmetadatachanged.IN_CLOSE_NOWRITEYesunwrittablefilewasclosed.IN_CLOSE_WRITEYeswrittablefilewasclosed.IN_CREATEYesfile/dirwascreatedinwatcheddirectory.IN_DELETEYesfile/dirwasdeletedinwatcheddirectory.IN_DELETE_SELFYes自删除,即一个可实行文件在实行时删除本人IN_DONT_FOLLOWNodontfollowasymlink(lk2.6.15).IN_IGNOREDYesraisedonwatcheditemremoving.Probablyuselessforyou,preferinsteadIN_DELETE*.IN_ISDIRNoeventoccurredagainstdirectory.Itisalwayspiggybackedtoanevent.TheEventstructureautomaticallyprovidethisinformation(via.is_dir)IN_MASK_ADDNotoupdateamaskwithoutoverwritingthepreviousvalue(lk2.6.14).Usefulwhenupdatingawatch.IN_MODIFYYesfilewasmodified.IN_MOVE_SELFYes自挪动,即一个可实行文件在实行时挪动本人IN_MOVED_FROMYesfile/dirinawatcheddirwasmovedfromX.CantracethefullmoveofanitemwhenIN_MOVED_TOisavailabletoo,inthiscaseifthemoveditemisitselfwatched,itspathwillbeupdated(seeIN_MOVE_SELF).IN_MOVED_TOYesfile/dirwasmovedtoYinawatcheddir(seeIN_MOVE_FROM).IN_ONLYDIRNoonlywatchthepathifitisadirectory(lk2.6.15).Usablewhencalling.add_watch.IN_OPENYesfilewasopened.IN_Q_OVERFLOWYeseventqueuedoverflowed.Thiseventdoesntbelongstoanyparticularwatch.IN_UNMOUNTYes宿主文件体系被umount
经由过程pyinotify来完成对文件体系的监控十分复杂
- #!/usr/bin/envpython#encoding:utf-8importosfrompyinotifyimportWatchManager,Notifier,ProcessEvent,IN_DELETE,IN_CREATE,IN_MODIFYclassEventHandler(ProcessEvent):"""事务处置"""defprocess_IN_CREATE(self,event):print"Createfile:%s"%os.path.join(event.path,event.name)defprocess_IN_DELETE(self,event):print"Deletefile:%s"%os.path.join(event.path,event.name)defprocess_IN_MODIFY(self,event):print"Modifyfile:%s"%os.path.join(event.path,event.name)defFSMonitor(path=.):wm=WatchManager()mask=IN_DELETE|IN_CREATE|IN_MODIFYnotifier=Notifier(wm,EventHandler())wm.add_watch(path,mask,rec=True)printnowstartingmonitor%s%(path)whileTrue:try:notifier.process_events()ifnotifier.check_events():notifier.read_events()exceptKeyboardInterrupt:notifier.stop()breakif__name__=="__main__":FSMonitor()
复制代码
[/code]
参考材料:
http://pyinotify.sourceforge.net/
http://trac.dbzteam.org/pyinotify/wiki
http://iyouf.info/pyinotify-monitor-filesystem.html
欢迎大家来到仓酷云论坛! |
|