Loop calls process that takes a long time to complete. How do I break out of it?

坚强是说给别人听的谎言 提交于 2019-12-12 02:44:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!