使用unix ksh shell脚本或perl脚本监视新文件的文件夹并触发perl脚本

我一直在谷歌search和溢出了一下,找不到任何可用的东西。

我需要一个监视公用文件夹的脚本,并在创build新文件时触发,然后将这些文件移动到一个私有位置。

我有一个桑巴共享文件夹/exam/ple/上的Unix映射到X:\在Windows上。 在某些操作中,txt文件被写入共享。 我想绑架出现在文件夹中的任何txt文件,并将其放置在unix上的私人文件夹/pri/vate中。 在移动文件后,我想触发一个单独的perl脚本。

编辑还在等待看到一个shell脚本,如果任何人有任何想法…将监视新文件,然后运行如下的东西:

 #!/bin/ksh mv -f /exam/ple/*.txt /pri/vate 

检查incron 。 它似乎正是你所需要的。

如果我理解正确,你只是想要这样的事情?

 #!/usr/bin/perl use strict; use warnings; use File::Copy my $poll_cycle = 5; my $dest_dir = "/pri/vate"; while (1) { sleep $poll_cycle; my $dirname = '/exam/ple'; opendir my $dh, $dirname or die "Can't open directory '$dirname' for reading: $!"; my @files = readdir $dh; closedir $dh; if ( grep( !/^[.][.]?$/, @files ) > 0 ) { print "Dir is not empty\n"; foreach my $target (@files) { # Move file move("$dirname/$target", "$dest_dir/$target"); # Trigger external Perl script system('./my_script.pl'); } } 

File :: ChangeNotify允许您监视文件和目录的更改。

https://metacpan.org/pod/File::ChangeNotify

我知道,我迟到了党,但为了完整性和为未来的访问者提供信息;

 #!/bin/ksh # Check a File path for any new files # And execute another script if any are found POLLPATH="/path/to/files" FILENAME="*.txt" # Or can be a proper filename without wildcards ACTION="executeScript.sh argument1 argument2" LOCKFILE=`basename $0`.lock # Make sure we're not running multiple instances of this script if [ -e /tmp/$LOCKFILE ] ; then exit 0 else touch /tmp/$LOCKFILE fi # check the dir for the presence of our file # if it's there, do something, if not exit if [ -e $POLLPATH/$FILENAME ] ; then exec $ACTION else rm /tmp/$LOCKFILE exit 0 fi 

从cron运行它;

*/1 7-22/1 * * * /path/to/poll-script.sh >/dev/null 2>&1

你想在你的后续脚本中使用lockfile($ ACTION),然后在退出时清理它,这样你就不需要任何堆栈了。

 $ python autocmd.py /exam/ple .txt,.html /pri/vate some_script.pl 

优点:

  • 由于pyinotify比incron更容易安装是纯Python
  • 事件驱动 – 比perl脚本的影响更小

autocmd.py :

 #!/usr/bin/env python """autocmd.py Adopted from autocompile.py [1] example. [1] http://git.dbzteam.org/pyinotify/tree/examples/autocompile.py Dependencies: Linux, Python, pyinotify """ import os, shutil, subprocess, sys import pyinotify from pyinotify import log class Handler(pyinotify.ProcessEvent): def my_init(self, **kwargs): self.__dict__.update(kwargs) def process_IN_CLOSE_WRITE(self, event): # file was closed, ready to move it if event.dir or os.path.splitext(event.name)[1] not in self.extensions: # directory or file with uninteresting extension return # do nothing try: log.debug('==> moving %s' % event.name) shutil.move(event.pathname, os.path.join(self.destdir, event.name)) cmd = self.cmd + [event.name] log.debug("==> calling %s in %s" % (cmd, self.destdir)) subprocess.call(cmd, cwd=self.destdir) except (IOError, OSError, shutil.Error), e: log.error(e) def process_default(self, event): pass def mainloop(path, handler): wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm, default_proc_fun=handler) wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True) log.debug('==> Start monitoring %s (type c^c to exit)' % path) notifier.loop() if __name__ == '__main__': if len(sys.argv) < 5: print >> sys.stderr, "USAGE: %s dir ext[,ext].. destdir cmd [args].." % ( os.path.basename(sys.argv[0]),) sys.exit(2) path = sys.argv[1] # dir to monitor extensions = set(sys.argv[2].split(',')) destdir = sys.argv[3] cmd = sys.argv[4:] log.setLevel(10) # verbose # Blocks monitoring mainloop(path, Handler(path=path, destdir=destdir, cmd=cmd, extensions=extensions)) 

这将导致一些公平的io – stat()调用等。 如果你想在没有运行时开销的情况下进行快速的通知(但是更多的前期工作),看看FAM / dnotify: 链接文本或链接文本

我不使用ksh,但是这里是我用sh做的。 我确定它很容易适应ksh。

 #!/bin/sh trap 'rm .newer' 0 touch .newer while true; do (($(find /exam/ple -maxdepth 1 -newer .newer -type f -name '*.txt' -print \ -exec mv {} /pri/vate \; | wc -l))) && found-some.pl & touch .newer sleep 10 done 
 #!/bin/ksh while true do for file in `ls /exam/ple/*.txt` do # mv -f /exam/ple/*.txt /pri/vate # changed to mv -f $file /pri/vate done sleep 30 done