如何在linux中创建文件监听器?

时间:2022-03-31 12:50:18

I am trying to create a listener on a specific directory that kicks off a Linux command whenever a file shows up in this path. For example, whenever a file shows up in a directory like C:/home/ I would like to read a string of text from that file and then kick off another command. I was thinking of using a loop but that seems inefficient.

我正在尝试在特定目录上创建一个侦听器,只要文件出现在此路径中,该目录就会启动Linux命令。例如,每当文件出现在像C:/ home /这样的目录中时,我想从该文件中读取一串文本,然后启动另一个命令。我正在考虑使用循环,但这似乎效率低下。

1 个解决方案

#1


11  

To get notified about events like file creation, opening, modifying etc. look into inotify. A good way to use it from bash is with the inotifywait command - here is its man page. It will block until an event you care about happens. For example:

要获得有关文件创建,打开,修改等事件的通知,请查看inotify。从bash中使用它的一个好方法是使用inotifywait命令 - 这是它的手册页。它会阻止,直到你关心的事件发生。例如:

inotifywait -e create /path/to/watch
echo "ding!"

will ding when a file or directory gets created in that path. See the man page for more details.

在该路径中创建文件或目录时将会发挥作用。有关更多详细信息,请参见手册页。

#1


11  

To get notified about events like file creation, opening, modifying etc. look into inotify. A good way to use it from bash is with the inotifywait command - here is its man page. It will block until an event you care about happens. For example:

要获得有关文件创建,打开,修改等事件的通知,请查看inotify。从bash中使用它的一个好方法是使用inotifywait命令 - 这是它的手册页。它会阻止,直到你关心的事件发生。例如:

inotifywait -e create /path/to/watch
echo "ding!"

will ding when a file or directory gets created in that path. See the man page for more details.

在该路径中创建文件或目录时将会发挥作用。有关更多详细信息,请参见手册页。