【问题标题】:bash excluding loop from fcntl locked filesbash 从 fcntl 锁定文件中排除循环
【发布时间】:2021-06-21 11:23:19
【问题描述】:

我有一个简单的脚本,它遍历文件夹中的文件,然后将它们从 flv 转换为 mp4。如何在 bash 中跳过文件夹中具有 fcntl 锁定的文件,然后在解除锁定时返回?


#!/bin/bash


for file in /video_recordings/stream/*.flv; do

  today=`date '+%Y_%m_%d_%H_%M_%S'`;

  cp -v "$file" /video_recordings/stream/backups/$today.flv;

  [ -e "${file%.flv}".mp4 ] || ffmpeg -threads 1 -i "$file" -c:v libx264 -c:a aac -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune fastdecode -preset ultrafast -crf 25  /video_recordings/stream/temp/$today.mp4

  cp -v /video_recordings/stream/temp/$today.mp4 /video_recordings/stream/vod/$today.mp4

  rm /video_recordings/stream/temp/$today.mp4

  sleep 15s;

  rm "$file";

done


【问题讨论】:

    标签: bash ffmpeg


    【解决方案1】:

    我对@9​​87654322@了解不多,但是根据this answer,这样的锁应该由fuser列出。如果有东西正在访问文件,fuser 将退出,状态码为 0,否则为 1。

    要稍后返回锁定的文件,请使用数组,就像它是一个队列一样:

    #!/bin/bash
    queue=(/video_recordings/stream/*.flv)
    while (( ${#queue[@]} > 0 )); do
      file="${queue[0]}"
      queue=("${queue[@]:1}")
      if fuser -s "$file"; then
        echo "$file in use. Try later."
        queue+=("$file")
        continue
      fi
      # insert commands for conversion of `$file` here
    done
    

    【讨论】:

    • 我的fuser 的版本也有-s--silent 标志,可能只需要说一下,我不知道fcntl 所以没有关于这个问题的答案: -)
    • 感谢您的评论。使用-s 选项确实是个好主意!
    猜你喜欢
    • 2021-08-14
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2014-04-25
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多