gpt4 book ai didi

linux - 如何知道文件何时添加到目录?

转载 作者:IT王子 更新时间:2023-10-29 01:06:35 27 4
gpt4 key购买 nike

在我们的 linux 服务器中,我们有一个在后台运行的程序,它在某个目录中创建文件。当一个新文件添加到该目录时,我想收到一封邮件。

我试过使用 Java,但那变得很复杂。所以我正在寻找一些更好的主意。是否有一些程序或脚本可以执行此操作?

最佳答案

好吧,我会矫枉过正(有这样的东西吗?)并建议使用 inotify-tools package 中的实用程序.

更具体地说 inotifywait工具:

# inotifywait -m /tmp
Setting up watches.
Watches established.
/tmp/ OPEN,ISDIR
/tmp/ CLOSE_NOWRITE,CLOSE,ISDIR
.
.
.

通过 grep 将其输出管道化,并将其发送到 Bash 循环或其他东西。瞧!

编辑:

这是一个快速而肮脏的单行代码:

inotifywait -m /tmp 2>/dev/null | grep --line-buffered '/tmp/ CREATE' | while read; do echo update | mail -s "/tmp updated" john@example.com; done

关于linux - 如何知道文件何时添加到目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com