2014-03-02 64 views
2

我有一个perl脚本,可将文件从一个传入目录分类到Ubuntu服务器上的其他目录。 因为现在我每隔几分钟就把它作为cron作业运行,但是如果脚本在文件写入传入目录时启动,它可能会出现问题。创建文件夹或文件时运行脚本

更好的解决方案是将文件写入传入目录或任何子目录时启动它。

我想我可以运行另一个脚本作为一个服务,将调用我的排序脚本,每当一个目录更改发生时,但我不知道如何去做这件事。

+0

https://github.com/seb-m/pyinotify/wiki –

+0

我有此相关的一个问题:https://github.com/seb -m/pyinotify/issues/128 – p1nox

回答

2

在Linux上,你可以使用pyinotify中库:https://github.com/seb-m/pyinotify

用于观看子目录,使用REC = true在add_watch()调用。完整的例子监控/ tmp目录及其文件创建子目录:

import pyinotify 

class EventHandler(pyinotify.ProcessEvent): 
    def process_IN_CREATE(self, event): 
     # Processing of created file goes here. 
     print "Created:", event.pathname 

wm = pyinotify.WatchManager() 

notifier = pyinotify.Notifier(wm, EventHandler()) 
wm.add_watch('/tmp', pyinotify.IN_CREATE, rec=True) 
notifier.loop() 
+0

非常感谢 – user3332151

相关问题