使用 inotifywait 进行监视和执行,但在队列中执行

使用 inotifywait 进行监视和执行,但在队列中执行

我有一个 rtorrent + rutorrent 设置,它将~/rtorrent/completed使用 autotools 插件将下载的 torrent 内容移动到。Inotifywait 在后台检查出现的任何新文件,~/rtorrent/completed然后使用该文件上传到 google driverclone copy

因此,一旦第一个 torrent 完成,然后它将文件移动到上面的文件夹,并且 rclone 开始上传,但问题是当第二个 torrent 完成并且它~/rtorrent/completed再次将内容移动到时,然后第一个上传过程中断。 Inotifywait 将此视为一个新事件。无论如何,我可以排队,这样就可以一一进行吗?

这是正在运行的 shell 脚本。

#!/bin/bash

cd ~/rtorrent/readytoupload/
inotifywait -m ~/rtorrent/readytoupload -s ~/rtorrent/log.txt -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        rclone copy $file tempd:test/$file
    done

答案1

几年前,我在工作中也做过类似的过程。我在 Linux 上有一个 samba 共享接受文件,目标是将视频文件从那里移动到另一个目录并处理它们,ffmpeg然后将其上传到 youtube。我无法发布脚本代码,因为我手头不再有它(我已经换了工作),但我可以给你一些提示:你需要向新文件发送一个管道 ( man mkfifo) 和一个辅助脚本来“读取“来自该管道的线路循环。

因此,您最终将得到至少两个脚本,一个用于运行inotifywait以将新文件名发送到管道,另一个脚本用于读取管道并触发您需要的进程(在您的情况下,使用 上传rclone)。添加的任何新文件都将放入管道中并等待该文件。第一个脚本必须持续监视新文件并尽快将它们发送到辅助脚本。

IE:

#!/bin/bash
cd ~/rtorrent/readytoupload/
inotifywait -m ~/rtorrent/readytoupload -s ~/rtorrent/log.txt -e create -e moved_to |
while read path action file; do
   echo "The file '$file' appeared in directory '$path' via '$action'"
   # I'm not entirely sure about the & placement here, if its neccesary at all,
   # but the goal is to prevent a block while the secondary script is working on another file.
   echo ${file} > /tmp/mypipe &
done

以及辅助脚本的示例:

#!/bin/bash
while true ; do
    if read line < /tmp/mypipe ; then
        echo $line
        # your rclone command goes here.
    fi
loop

大括号是为了防止带空格的文件名被解释为多个参数。你应该这样引用它们"${file}"

如果您想在启动计算机后触发脚本,您将需要第三个脚本来完成这项工作,我建议使用su -c&触发守护程序脚本或从(或)rc.local运行任何类似的脚本。/etc/rc.d/etc/init.d

相关内容