使用inotify-tools和ruby将上传推送到云文件
我编写了一些脚本来监视上传目录的更改,然后捕获上传/更改的文件并使用 ruby 脚本将其推送到云文件。这在 95% 的情况下都运行良好,唯一的例外是 ruby 偶尔会失败并出现“文件不存在”异常。
我假设在文件 100% 位于新位置之前调用 ruby“push”脚本,因此该脚本被调用得有点过早。
我尝试在我的脚本中添加一个小函数来检查文件是否存在,如果不存在,则睡眠 5 分钟,然后重试,但这似乎会滚雪球般地滚雪球并最终消失。然后我只是在所有调用中添加了 sleep 2 ,但这并没有帮助,因为我现在再次收到“文件不存在”错误。
#!/bin/sh
function checkExists {
if [ ! -e "$1" ]
then
sleep 5
checkExists $1
fi
}
inotifywait -mr --timefmt '%d/%m/%y-%H:%M' --format '%T %w %f' -e modify,moved_to,create,delete /home/skylines/html/forums/uploads | while read date dir file; do
cloudpath=${dir:20}${file}
localpath=${dir}${file}
#checkExists $localpath
sleep 2
ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath
echo "${date} ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath" >> /var/log/pushToCloud.log
done
我正在寻找任何建议来帮助我使其 100% 稳定(最终,我将提供从 Cloud FIles 上传的文件,所以我需要确保其完美)
提前致谢!
编辑 - 已解决!! - 我正在观看“删除”事件...因此,当本地文件被删除(不存在)时,此脚本将被触发。德尔。 :)
I wrote a few scripts to monitor an uploads directory for changes, then capture the file uploaded/changed and push it to cloud files using a ruby script. This all works well 95% of the time, the only exception is that occasionally, ruby fails with a 'file does not exist' exception.
I am assuming that the ruby 'push' script is being called before the file is 100% in its new location, so the script is being called a little prematurely.
I tried adding a little function to my script to check if the file exists, if it doesn't, sleep 5 then try again, but this seems to snowball and eventually dies. I then just added a sleep 2 to all calls, but it hasn't helped as I now get the 'file does not exist' error again.
#!/bin/sh
function checkExists {
if [ ! -e "$1" ]
then
sleep 5
checkExists $1
fi
}
inotifywait -mr --timefmt '%d/%m/%y-%H:%M' --format '%T %w %f' -e modify,moved_to,create,delete /home/skylines/html/forums/uploads | while read date dir file; do
cloudpath=${dir:20}${file}
localpath=${dir}${file}
#checkExists $localpath
sleep 2
ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath
echo "${date} ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath" >> /var/log/pushToCloud.log
done
I am looking for any suggestions to help me make this 100% stable (eventually, I'll serve the uploaded files from Cloud FIles, so I need to make sure its perfect)
Thanks in advance!
EDIT - solved!! - I am watching the 'delete' event... So, this script is firing when local files have been deleted, which dont exist. Derr. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定它不是上传文件中的空格之类的愚蠢的东西吗?尝试在传递给脚本的参数周围添加引号!
Have you made sure it's not something silly as a space in the uploaded file? Try adding quotes around the parameters passed to your script!
您的函数递归地调用自身而不退出。尝试将其更改为使用
while
循环。Your function calls itself recursively with no exit. Try changing it to use a
while
loop instead.