持续监视linux中的目录并在新文件可用时通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18692134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:46:56  来源:igfitidea点击:

Continuously monitor a directory in linux and notify when a new file is available

linuxshellservice

提问by HRM

I'm a starter in Linux and scripting environment. My requirement is like this:

我是 Linux 和脚本环境的初学者。我的要求是这样的:

  1. From an asp.net application, a file will be generated and copied to a predefined folder on a Linux server machine. (I'm assuming this can be done by remote file sharing using samba server)

  2. A service or script or whatever should be there in Linux machine to track continuously whether the file is available.

  3. Once a new file is available, just parse the file, extract some input variables and execute a shell script based on these parameters.

  1. 从 asp.net 应用程序中,将生成一个文件并将其复制到 Linux 服务器计算机上的预定义文件夹。(我假设这可以通过使用 samba 服务器的远程文件共享来完成)

  2. Linux 机器中应该有一个服务或脚本或任何东西来持续跟踪文件是否可用。

  3. 一旦有新文件可用,只需解析该文件,提取一些输入变量并根据这些参数执行 shell 脚本。

My question lies in point no:2. --> How can I write a service or script which should execute continuously and monitor whether a file is available in a particular folder?

我的问题在于第 2 点。--> 如何编写应连续执行并监视文件在特定文件夹中是否可用的服务或脚本?

I've searched a lot, got into a lot of links and I'm confused what is the easiestmethod to do this. Because I don't want to spend a lot of coding here as the script to be executed further and the asp.net app is more important and this should be a connector in between.

我搜索了很多,进入了很多链接,我很困惑什么是最简单的方法来做到这一点。因为我不想在这里花费大量编码作为要进一步执行的脚本,而 asp.net 应用程序更重要,这应该是介于两者之间的连接器。

采纳答案by cnicutar

You are looking for something like inotify.

你正在寻找类似的东西inotify

[cnicutar@ariel ~]$ inotifywait -m -e create ~/somedir/
Setting up watches.
Watches established.
/home/cnicutar/somedir/ CREATE somefile

For example, you could do it in a loop:

例如,您可以在循环中执行此操作:

inotifywait -m -e create ~/somedir/ | while read line
do
    echo $line
done

回答by necromancer

inotifyis the perfect solution to your problem. It is available as a system command that can be used in a shell, or as a system call that can be used in a C/C++ program. For details see the accepted answer to this question: Inotify - how to use it? - linux

inotify是您问题的完美解决方案。它可以作为可以在 shell 中使用的系统命令,也可以作为可以在 C/C++ 程序中使用的系统调用。有关详细信息,请参阅此问题的公认答案:Inotify - 如何使用它?- linux

Update:you need inotify-toolsfor use on command line. The answer to the question above only describes C/C++ system calls. Here is the link to inotify-tools. It is also available as a packaged distribution so search your favorite install repository (yum/apt-get/rpmetc.): https://github.com/rvoicilas/inotify-tools/wiki

更新:您需要inotify-tools在命令行上使用。上述问题的答案仅描述了 C/C++ 系统调用。这是 inotify-tools 的链接。它也可以作为打包分布,使搜索自己喜欢的安装库(yum/ apt-get/rpm等):https://github.com/rvoicilas/inotify-tools/wiki