9

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

我需要一个脚本来监视公共文件夹并在新文件创建时触发,然后将文件移动到私人位置。

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

编辑 如果有人有任何想法,仍在等待查看 shell 脚本......将监视新文件然后运行类似的东西:

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

8 回答 8

9

检查incron。它似乎完全符合您的需要。

于 2009-10-07T20:30:46.257 回答
6

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

#!/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');
    }
}
于 2009-10-07T20:38:56.427 回答
5

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

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

于 2009-10-07T23:58:35.487 回答
3

我知道我参加聚会迟到了,但为了完整起见并向未来的访客提供信息;

#!/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 ) 中使用锁定文件,然后在退出时将其清理,这样您就没有任何堆叠过程。

于 2013-02-28T08:29:56.887 回答
2
$ python autocmd.py /exam/ple .txt,.html /pri/vate some_script.pl

好处:

自动命令.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))
于 2009-10-08T19:52:22.310 回答
1

这将导致相当多的 io - stat() 调用等。如果您想要快速通知而不需要运行时开销(但需要更多前期工作),请查看 FAM/dnotify:链接文本链接文本

于 2009-10-08T01:29:15.063 回答
1

我不使用 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
于 2011-01-15T22:01:34.493 回答
0
#!/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
于 2009-10-08T14:58:08.287 回答