用python监视文件变化,定时更新修改过的文件。-- 逆水寒

来源:互联网 发布:矩阵分块 编辑:程序博客网 时间:2024/06/10 19:05
#!/usr/bin/env python# -*- coding: UTF-8 -*-import osimport sysimport timeimport urllibimport pyinotifyimport loggingimport subprocessfrom pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEventsync_dirs = {'/mnt/video/pvmsvideo/2013'                :  'pvms_video_pvmsvideo',\             '/mnt/video/pvmsimage/2013'                :  'pvms_video_pvmsimage'}def Monitor():        class Ptmp(ProcessEvent):                """docstring for Ptmp"""                def process_IN_CREATE(self,event):passdef process_IN_CLOSE_WRITE(self,event):                        print "Modify : %s " % os.path.join(event.path,event.name)                        #notify_change(event)                def process_IN_DELETE(self,event):fp = open("/var/log/lostvideo.log","w+")                        fp.write("Remove : %s " % os.path.join(event.path,event.name))fp.close()        wm = WatchManager()        mask = pyinotify.IN_CREATE|pyinotify.IN_DELETE|pyinotify.IN_CLOSE_WRITE        notifier = Notifier(wm,Ptmp())        pathes = sync_dirs.keys()        for path in pathes:                wdd = wm.add_watch(path,mask,rec=True,auto_add=True)        while True:                try:                        notifier.process_events()                        if notifier.check_events():                                notifier.read_events()                except KeyboardInterrupt:                        notifier.stop()                        breakdef notify_change(event):        for key in sync_dirs.keys():                if event.path.find(key) >= 0 :ps_cmd = "ps -ef|grep -i \"rsync.*%s.*\"|wc -l" % (key)print "%s %d" % (ps_cmd,int(os.popen(ps_cmd).read()))if int(os.popen(ps_cmd).read()) == 2:                        cmd = "rsync -avrP --chmod=Du=rwx,Dg=rx,Do=rx,Fu=rw,Fg=r,Fo=r --password-file=/etc/backup.pass %s --exclude-from=/etc/rsyncexclude.list backup@10.211.1.115::%s & " % (key,sync_dirs[key])                        print cmd                        #os.system(cmd)subprocess.Popen(cmd,shell=True)if __name__ == "__main__":        Monitor()
能不能抽时间帮忙看看 怎么加个定时器到上边代码,
需求是定时每20分钟同步一次修改过的文件到另一台服务器。
原创粉丝点击