简体   繁体   English

一个监控Linux目录的程序

[英]a program to monitor a directory on Linux

There is a directory where a buddy adds new builds of a product. 有一个伙伴添加产品的新版本的目录。

The listing looks like this 列表看起来像这样

$ ls path-to-dir/
01
02
03
04
$

where the numbers listed are not files but names of directories containing the builds. 列出的数字不是文件,而是包含构建的目录的名称。

I have to manually go and check every time whether there is a new build or not. 我必须每次手动去检查是否有新的版本。 I am looking for a way to automate this, so that the program can send an email to some people (including me) whenever path-to-dir/ is updated. 我正在寻找一种自动化方法,以便程序可以在path-to-dir/更新时向某些人(包括我)发送电子邮件。

  • Do we have an already existing utility or a Perl library that does this? 我们是否有一个已经存在的实用程序或Perl库来执行此操作?

    inotify.h does something similar, but it is not supported on my kernel (2.6.9). inotify.h做了类似的事情,但我的内核不支持它(2.6.9)。

I think there can be an easy way in Perl. 我认为在Perl中可以有一个简单的方法。

  • Do you think this will work? 你认为这会奏效吗?

    Keep running a loop in Perl that does a ls path-to-dir/ after, say, every 5 minutes and stores the results in an array. 继续在Perl中运行一个循环,它执行ls path-to-dir/ after,比如每5分钟一次,并将结果存储在一个数组中。 If it finds that the new results are different from the old results, it sends out an email using Mail or Email . 如果发现新结果与旧结果不同,则会使用邮件电子邮件发送电子邮件

If you're going for perl, I'm sure the excellent File::ChangeNotify module will be extremely helpful to you. 如果您要使用perl,我确信优秀的File::ChangeNotify模块对您非常有帮助。 It can use inotify , if available, but also all sorts of other file-watching mechanisms provided by different platforms. 它可以使用inotify (如果可用),还可以使用不同平台提供的各种其他文件监视机制。 Also, as a fallback, it has its own watching implementation, which works on every platform, but is less efficient than the specialized ones. 此外,作为后备,它有自己的观察实现,适用于每个平台,但效率低于专业平台。

Checking for different ls output would send a message even when something is deleted or renamed in the directory. 即使在目录中删除或重命名某些内容,检查不同的ls输出也会发送消息。 You could instead look for files with an mtime newer than the last message sent. 您可以使用比上次发送的消息更新的mtime来查找文件。

Here's an example in bash, you can run it every 5 minutes: 这是bash中的一个示例,您可以每5分钟运行一次:

now=`date +%Y%m%d%H%M.%S`

if [ ! -f "/path/to/cache/file" ] || [ -n "`find /path/to/build/dir -type f -newer /path/to/cache/file`" ]
then
    touch /path/to/cache/file -t "$now"
    sendmail -t <<< "
To: aaa@bbb.ccc
To: xxx@yyy.zzz
Subject: New files found

Dear friend,
I have found a couple of new files.
"
fi

Can't it be a simple shell script? 它不能是一个简单的shell脚本吗?

while :;do
        n = 'ls -al path-to-dir | wc -l'
        if n -gt old_n
    # your Mail code here; set old_n=n also
        fi
   sleep 5
done

Yes, a loop in Perl as described would do the trick. 是的,所描述的Perl中的循环可以解决问题。

You could keep a track of when the directory was last modified; 您可以跟踪上次修改目录的时间; if it hasn't changed, there isn't a new build. 如果它没有改变,那么就没有新的版本。 If it has changed, an old build might have been deleted or a new one added. 如果已更改,则可能已删除旧版本或添加了新版本。 You probably don't want to send alerts when old builds are removed; 您可能不希望在删除旧版本时发送警报; it is crucial that the email is sent when new builds are added. 添加新版本时发送电子邮件至关重要。

However, I think that msw has the right idea; 但是,我认为msw有正确的想法; the build should notify when it has completed the copy out to the new directory. 构建应该在完成复制到新目录时通知。 It should be a script that can be changed to notify the correct list of people - rather than a hard-wired list of names in the makefile or whatever other build control system you use. 它应该是一个可以更改的脚本,以通知正确的人员列表 - 而不是makefile中的硬连线名称列表或您使用的任何其他构建控件系统。

you could use dnotify it is the predecessor of inotify and should be available on your kernel. 你可以使用dnotify它是inotify的前身,应该可以在你的内核上使用。 It is still supported by newer kernels. 它仍然受到更新内核的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM