我一直在谷歌上搜索满溢了一下,没找到什么有用。
我需要监视公用文件夹和触发器创建新文件,然后将文件移动到一个私人的位置的脚本。
我有一个桑巴共享文件夹/exam/ple/
UNIX上映射到X:\
上的窗户。 在某些操作,TXT文件写入到共享。 我想绑架在出现的文件夹中的任何txt文件,并把它变成一个私有文件夹/pri/vate
在UNIX上。 该文件被移动后,我想触发一个单独的Perl脚本。
编辑还在观望shell脚本,如果任何人有任何想法...的东西,将监测的新文件,然后运行是这样的:
#!/bin/ksh
mv -f /exam/ple/*.txt /pri/vate
如果我理解正确的话,你只是想这样的事情?
#!/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');
}
}
文件:: 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
你想使用锁文件在随后的脚本($ ACTION),然后清理退出,只是让你没有任何堆放过程。
$ python autocmd.py /exam/ple .txt,.html /pri/vate some_script.pl
好处:
- 更容易安装比
incron
由于pyinotify中是纯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
文章来源: Monitor folder for new files using unix ksh shell script or perl script and trigger perl script