【问题标题】:monitor a folder tree for changes and run a script when a file is created - linux监视文件夹树的更改并在创建文件时运行脚本 - linux
【发布时间】:2014-07-11 05:47:01
【问题描述】:

我正在尝试创建一个后台运行的 shell 脚本,它会在文件树中创建文件时通知它,检查文件名是否符合特定条件,通过电子邮件发送文件,然后移动它。

应用程序 - 通过电子邮件发送 FreePBX 通话录音。

文件存储在 /var/spool/asterisk/monitor/yyyy/mm/dd/

例如 - /var/spool/asterisk/monitor/2014/07/10/conf-220-220...

因此,当创建记录时,它会根据文件名检查它的去向、发送电子邮件并将其移出文件夹。

我看到了有关 inotify-tools 的建议,但该网站上的最新消息是 2010 年的。

我也看到了 incron,但想要一些可能会破坏我的电话系统的东西(我害怕安装程序)

inotify.aiken.cz/?section=incron&page=doc&lang=en

因为这个版本比较早,它不包含标准的可移植构建机制(例如用于自动工具)。只有一个 Makefile 必须手动修改。在许多 Linux 系统上,您无需更改任何内容。

【问题讨论】:

    标签: linux shell


    【解决方案1】:

    inotify 已经存在了一段时间,它很稳定并且是许多发行版主流的一部分。

    如何安装:

    在 Ubuntu 上:

    sudo apt-get install inotify-tools

    在 Centos/RHEL 上(来自 EPEL 存储库):

    yum --enablerepo epel install inotify-tools

    使用方法:

    inotifywait -re create /tmp/test1/ && echo "Change detected"
    

    创建文件后,echo "change detected" 将被触发,这几乎可以是任何东西。

    你得到的输出:

    Setting up watches.  Beware: since -r was given, this may take a while!
    Watches established.
    /tmp/test1/test2/test3/ CREATE file
    Change detected
    

    PS。 -r 用于递归 -e create 以检测新文件。

    【讨论】:

    • 谢谢,这是我的脚本:!/bin/shwhile true; doinotifywait -re close_write /var/spool/asterisk/monitor//usr/local/bin/send_rec.shdone
    猜你喜欢
    • 2018-02-15
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多