问题
I got to this solution yesterday with help from here (it's finding a list of directories to process with HandBrakeCLI, and putting the results in a hard-coded directory):
#!/bin/bash
source_dir=/Volumes/VolumeName
dest_dir=/Volumes/OtherName
export dest_dir # export allows use by subprocesses!
find "$source_dir" -type d -name "VIDEO_TS" -exec bash -c '
for dir; do
name=${dir%/VIDEO_TS}
name=${name##*/}
./HandBrakeCLI -i "$dir" -o "$dest_dir/$name.m4v"
done
' _ {} +
The shell script works fine, but if I try to stop it by killing the main process:
kill -9 <pid>
it seems to work but then comes back to life and calls HandBrakeCLI with the next file in the list generated by find.
Is there a way of reliably stopping the whole thing? I'm assuming that somehow the future calls to the CLI are being queued/cached, but could someone explain what's happening please?
回答1:
Your script is made unnecessarily complex by invoking bash from find. I suggest iterating over the output of find.
#!/usr/bin/env bash
source_dir=/Volumes/VolumeName
dest_dir=/Volumes/OtherName
find "${source_dir}" -type d -name "VIDEO_TS" | while read dir; do
name=${dir%/VIDEO_TS}
name=${name##*/}
./HandBrakeCLI -i "${dir}" -o "${dest_dir}/${name}.m4v"
done
回答2:
I discovered that there's a problem with using find piped to while loop as suggested by @JonNailey with HandBrakeCLI (and other processes that call ffmpeg): it 'sucks up' all the input from the find command and starves the loop. See http://mywiki.wooledge.org/BashFAQ/089 for details.
The solution I chose was to put this redirection on the line calling HandBrakeCLI:
</dev/null
来源:https://stackoverflow.com/questions/35070552/loop-calls-process-that-takes-a-long-time-to-complete-how-do-i-break-out-of-it