我有一个 Windows 应用程序将创建一个 .xml 文件并将其上传到我的 Linux 服务器上的一个文件夹中。我需要一种方法来监视这个新文件,它的文件名类似于“004job.xml”、“012job.xml”等。文件名每次都不一样。检测到文件后,将使用新文件作为其命令行变量运行脚本。像这样“/home/project/scripts/renderjob.sh/home/project/jobs/012job.xml”。脚本会根据.xml文件的内容做一些 Action ,最后的 Action 是删除.xml文件。
同样重要的是,一次只运行此脚本的一个实例。但那是一匹不同颜色的马。
感谢阅读。
最佳答案
简要说明:
After the file is detected, a script would be run with the new file as its command line variable.
Its also important that only one instance of this script is running at a time.
以这种方式在脚本中使用 flock
:https://unix.stackexchange.com/questions/274498/performing-atomic-write-operations-in-a-file-in-bash/274499#274499 .在 flock 中插入监视你的文件:
(
flock -s 200
res=$(inotifywait $DIR)
# other actions
) 200>/var/lock/myscriptlock
关于xml - 如何查看文件夹中的新文件,然后将新文件作为 $1 执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102579/