linux - 监视新文件的目录,然后运行脚本 [英] linux - watch a directory for new files, then run a script

查看:53
本文介绍了linux - 监视新文件的目录,然后运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Ubuntu 14.04 中观察一个目录,当在这个目录中创建一个新文件时,运行一个脚本.

I want to watch a directory in Ubuntu 14.04, and when a new file is created in this directory, run a script.

具体来说,我有安全摄像头,当它们检测到运动时,它们会通过 FTP 捕获的视频上传.我想在这个 FTP 服务器上运行一个脚本,这样当创建新文件时,它们会立即镜像(上传)到云存储服务,这是通过我已经编写的脚本完成的.

specifically I have security cameras that upload via FTP captured video when they detect motion. I want to run a script on this FTP server so when new files are created, they get mirrored (uploaded) to a cloud storage service immediately, which is done via a script I've already written.

我发现 iWatch 可以让我这样做 (http://iwatch.sourceforge.net/index.html) - 我遇到的问题是 iwatch 在 FTP 目录中创建文件的那一刻立即启动云上传脚本,即使文件仍在上传.这会导致云同步脚本上传 0 字节的文件,对我没用.

I found iWatch which lets me do this (http://iwatch.sourceforge.net/index.html) - the problem I am having is that iwatch immediately kicks off the cloud upload script the instant the file is created in the FTP directory, even while the file is in progress of being uploaded still. This causes the cloud sync script to upload 0-byte files, useless to me.

我可以在云上传脚本中添加一个等待",但它看起来很老套,无法预测等待多长时间,因为它取决于文件大小、网络条件等.

I could add a 'wait' in the cloud upload script maybe but it seems hack-y and impossible to predict how long to wait as it depends on file size, network conditions etc.

有什么更好的方法来做到这一点?

Whats a better way to do this?

推荐答案

虽然评论中提到了 inotifywait,但完整的解决方案可能对其他人有用.这似乎有效:

Although inotifywait was mentioned in comments, a complete solution might be useful to others. This seems to be working:

 inotifywait -m -e close_write /tmp/upload/ | gawk '{print $1$3; fflush()}' | xargs -L 1 yourCommandHere

将运行

  yourCommandHere /tmp/upload/filename

当一个新上传的文件关闭时

when a newly uploaded file is closed

注意事项:

  • inotifywait 是 Ubuntu 中 apt 包 inotify-tools 的一部分.它使用内核 inotify 服务来监控文件或目录事件
  • -m 选项为监控模式,每个事件输出一行到标准输出
  • -e close_write 用于打开文件的文件关闭事件以进行写入.文件关闭事件有望避免收到不完整的文件.
  • /tmp/upload 可以替换成其他目录来监控
  • gawk 的管道重新格式化 inotifywait 输出行以删除第二列,这是事件类型的重复.它将第 1 列中的目录名与第 3 列中的文件名组合成一个新行,每行刷新一行以破坏缓冲并鼓励 xargs
  • 立即采取行动
  • xargs 获取文件列表并为每个文件运行给定的命令,并在命令末尾附加文件名.-L 1 使 xargs 在标准输入接收到每一行后运行.
  • inotifywait is part of apt package inotify-tools in Ubuntu. It uses the kernel inotify service to monitor file or directory events
  • -m option is monitor mode, outputs one line per event to stdout
  • -e close_write for file close events for files that were open for writing. File close events hopefully avoid receiving incomplete files.
  • /tmp/upload can be replaced with some other directory to monitor
  • the pipe to gawk reformats the inotifywait output lines to drop the 2nd column, which is a repeat of the event type. It combines the dirname in column 1 with the filename in column 3 to make a new line, which is flushed every line to defeat buffering and encourage immediate action by xargs
  • xargs takes a list of files and runs the given command for each file, appending the filename on the end of the command. -L 1 causes xargs to run after each line received on standard input.

这篇关于linux - 监视新文件的目录,然后运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆