0

I have a simple script that loops through files in a folder and then converts them from flv to mp4. How in bash do I skip over the files in the folder that have a fcntl lock on them and then come back when the lock is removed?


#!/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


johnsonjp34
  • 3,139
  • 5
  • 22
  • 48

1 Answers1

1

I don't know much about fcntl, but according to this answer such locks should be listed by fuser. If something is accessing a file fuser exits with status code 0 and 1 otherwise.

To come back to the locked files later use an array as if it was a queue:

#!/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
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • Also the version of my `fuser` has the `-s` or `--silent` flag, might be needed just sayin, I have no idea about `fcntl` so no answer for me about this question :-) – Jetchisel Mar 24 '21 at 20:49
  • 1
    Thanks for the comment. Using the `-s` option is a great idea indeed! – Socowi Mar 24 '21 at 21:31