Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing a child process (HandbrakeCLI) from causing the parent script to exit

Tags:

bash

macos

I have a batch conversion script to turn .mkvs of various dimensions into ipod/iphone sized .mp4s, cropping/scaling to suit. Determining the original dimensions, required crop, output file is all working fine. However, on successful completion of the first conversion, HandbrakeCLI causes the parent script to exit. Why would this be? And how can I stop it?

The code, as it currently stands:

#!/bin/bash
find . -name "*.mkv" | while read FILE
do
    # What would the output file be?
    DST=../Touch/$(dirname "$FILE")
    MKV=$(basename "$FILE")
    MP4=${MKV%%.mkv}.mp4

    # If it already exists, don't overwrite it
    if [ -e "$DST/$MP4" ]
    then
        echo "NOT overwriting $DST/$MP4"
    else

        # Stuff to determine dimensions/cropping removed for brevity

        HandbrakeCLI --preset "iPhone & iPod Touch" --vb 900 --crop $crop -i "$FILE" -o "$DST/$MP4" > /dev/null 2>&1

        if [ $? != 0 ]
        then
            echo "$FILE had problems" >> errors.log  
        fi
     fi
done

I have additionally tried it with a trap, but that didn't change the behaviour (although the last trap did fire)

trap "echo Handbrake SIGINT-d"  SIGINT
trap "echo Handbrake SIGTERM-d" SIGTERM
trap "echo Handbrake EXIT-d"    EXIT
trap "echo Handbrake 0-d"       0

Edited to add:

The fact that the '0' trap fired prompted me to investigate why that might be. Executing as bash -x $script revealed that the find | while read loop is ending prematurely.

I've re-factored the find and encode into separate scripts. The find loop is now:

find . -name "*.mkv" | while read FILE
do
    handbrake-touch "$FILE"

    if [ $? != 0 ]
    then
        echo "$FILE had problems" >> errors.log  
    fi
done

The behaviour remains the same - one encode followed by the while loop ending. If I just put an 'echo $FILE' in place of the 'handbrake-touch', all the files are listed. The current directory doesn't change (I was wondering what might break the find | while).

like image 609
Chris Avatar asked Apr 25 '10 11:04

Chris


1 Answers

Now resolved, thanks to a clue from this other thread. I now echo nothing into HandbrakeCLI to ensure it's not using the same stdin as my script:

find . -name "*.mkv" | while read FILE
do
    echo "" | handbrake-touch "$FILE"

    if [ $? != 0 ]
    then
        echo "$FILE had problems" >> errors.log  
    fi
done

...and it works as intended/expected.

like image 114
Chris Avatar answered Nov 11 '22 20:11

Chris