Daniel Peters
Daniel Peters

Reputation: 3

Looping of bash script

Once I start this convertscript.sh I want it to constantly check the files in the directory, if theres an .mkv it will convert it which it does fine at the moment, but when there are no mkv files left it seems to exit out the script, but I want it to loop and keep trying the script constantly.

Basically if a new file gets added say during the night because the script is looping constantly it picks it up, I don't want to run the convertscript.sh again. I want to run convertscript.sh once initially and then not touch it.

The script I've got so far is below:

shopt -s globstar
for f in **/*.mkv
do 
    ffmpeg -i "$f" -c:v libx264 -preset ultrafast -minrate 4.5M -maxrate 4.5M -bufsize 9M -c:a ac3 "${f%mkv}mp4";
    [[ $? -eq 0 ]] && rm "$f";
done

Upvotes: 0

Views: 40

Answers (1)

tripleee
tripleee

Reputation: 189317

If you want to loop forever, you have to say so.

shopt -s globstar
while true; do
    for f in **/*.mkv; do 
        ffmpeg -i "$f" -c:v libx264 -preset ultrafast \
            -minrate 4.5M -maxrate 4.5M -bufsize 9M \
            -c:a ac3 "${f%mkv}mp4" &&
        rm "$f"
    done
    # Don't consume CPU by looking for new files immediately
    sleep 1
done

Notice also how we avoid the $? antipattern. Maybe increase the sleep to five minutes or so once you are confident that this is working as planned.

Upvotes: 1

Related Questions