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

时间:2021-12-21 17:00:13

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)

    从asp.net应用程序生成文件并复制到Linux服务器机器上的预定义文件夹中。(我假设这可以通过使用samba服务器进行远程文件共享来实现)

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

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

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

    一旦一个新文件可用,只需解析该文件,提取一些输入变量,并基于这些参数执行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 easiest method 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应用程序更重要,这应该是介于两者之间的连接器。

2 个解决方案

#1


20  

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

#2


2  

inotify is 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-tools for 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/rpm etc.): https://github.com/rvoicilas/inotify-tools/wiki

更新:您需要inotify工具在命令行上使用。上面问题的答案只描述了C/ c++系统调用。下面是inotify工具的链接。它也可以作为打包的发行版来使用,因此可以搜索您最喜欢的安装存储库(yum/apt-get/rpm等):https://github.com/rvoicilas/inotify-tools/wiki

#1


20  

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

#2


2  

inotify is 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-tools for 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/rpm etc.): https://github.com/rvoicilas/inotify-tools/wiki

更新:您需要inotify工具在命令行上使用。上面问题的答案只描述了C/ c++系统调用。下面是inotify工具的链接。它也可以作为打包的发行版来使用,因此可以搜索您最喜欢的安装存储库(yum/apt-get/rpm等):https://github.com/rvoicilas/inotify-tools/wiki