Linux\Windows 如何监控目录中的文件变化

一、背景

  作为一名工程师必须要有强悍的利器在手、不要做机器的奴隶 “ 工欲善其事,必先利其器”。为啥写脚本呢,因为在Linux没找到个顺手的工具(知道的伙伴请留言):

二、环境准备:

  带有python 系统电脑的机器一个 

  安装python2.7 或者  3.6  都行

三、代码如下

python2.7 版本  python 3版本看下一段
 #!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import datetime
import pyinotify
import logging

class MyEventHandler(pyinotify.ProcessEvent):

      logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log')
        #自定义写入那个文件,可以自己修改
      logging.info("Starting monitor...")

       

      def process_IN_ACCESS(self, event):
          print "ACCESS event:", event.pathname
          logging.info("ACCESS event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

   

      def process_IN_ATTRIB(self, event):
          print "ATTRIB event:", event.pathname
          logging.info("IN_ATTRIB event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))


      def process_IN_CLOSE_NOWRITE(self, event):
          print "CLOSE_NOWRITE event:", event.pathname
          logging.info("CLOSE_NOWRITE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
   

      def process_IN_CLOSE_WRITE(self, event):
          print "CLOSE_WRITE event:", event.pathname
          logging.info("CLOSE_WRITE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
   

      def process_IN_CREATE(self, event):
          print "CREATE event:", event.pathname
          logging.info("CREATE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))

      def process_IN_DELETE(self, event):
          print "DELETE event:", event.pathname
          logging.info("DELETE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
   

      def process_IN_MODIFY(self, event):
          print "MODIFY event:", event.pathname
          logging.info("MODIFY event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
   

      def process_IN_OPEN(self, event):
          print "OPEN event:", event.pathname
          logging.info("OPEN event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
       


def main():

    # watch manager
    wm = pyinotify.WatchManager()
    wm.add_watch('/www/wwwroot/www_jutaozhan_com/public/upload/', pyinotify.ALL_EVENTS, rec=True)
    #/tmp是可以自己修改的监控的目录
    # event handler
    eh = MyEventHandler()
    # notifier
    notifier = pyinotify.Notifier(wm, eh)
    notifier.loop()


if __name__ == '__main__':
    main()

你可能感兴趣的:(Linux\Windows 如何监控目录中的文件变化)