我写了一个小的 Bash 脚本,它使用 inotify-tools 和 inotify 接口。我的问题是,这个函数中的一个命令可以阻止执行,直到它完成。这样功能就会卡住。
为了解决这个问题,我想将检测到的文件排队(通过关闭事件),并从另一个函数中读取队列。有人知道如何在 Bash 中执行此操作吗?
下面的变量是用于查找目录或分配文件名的简单字符串。
inotifywait -mrq -e close --format %w%f /some/dir/ | while read FILE
do
NAME=$(echo $CAP)_"`date +"%F-%H-%M-%S"`.pcap"
logger -i "$FILE was just closed"
# cp "$FILE" "$DATA/$CAP/$ENV/$NAME"
rsync -avz --stats --log-file=/root/rsync.log "$FILE" "$DATA/$CAP/$ENV/$NAME" >> /root/rsync_stats.log
RESULT=$?
if [ $RESULT -eq 0 ] ; then
logger -i "Success: $FILE copied to SAN $DATA/$CAP/$ENV/$NAME, code $RESULT"
else
logger -i "Fail: $FILE copy failed to SAN for $DATA/$CAP/$ENV/$NAME, code $RESULT"
fi
rm "$FILE"
RESULT=$?
if [ $RESULT -eq 0 ] ; then
logger -i "Success: deletion successfull for $FILE, code $RESULT"
else
logger -i "Fail: deletion failed for $FILE on SSD, code $RESULT"
fi
do_something()
logger -i "$NAME was handled"
# for stdout
echo "`date`: Moved file"
done
我正在将文件复制到 SAN 卷,该卷有时会出现响应时间变化。这就是为什么这个功能会卡住一段时间的原因。我用 Rsync 替换了 cp 因为我需要吞吐量统计信息。Cp(来自 coreutils)显然没有这样做。