MEncoder & Youtube-upload Scripts retry if they fail

0

I have a script that encodes a video every day (cron job) and this video is uploaded to youtube just after its creation. Although sometimes these scripts fail.

How can i make the script to retry these two commands if one of them or both fail ?

The script:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

echo $(date)
cd /home/pi/
ls *.jpg > stills.txt

DATE=$(date --date=yesterday +"%Y-%m-%d")
three_days_ago=$(date --date="3 days ago" "+%m-%d")
yesterday=$(date --date yesterday "+%m-%d")
video_name=$(date +"%m-%d")


mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o $yesterday.avi -mf type=jpeg:fps=10 mf://@stills.txt

video_link=$(youtube-upload --email=xxx --password=xxx--title="Timelapse Video of "$yesterday --description="Heraklion Timelapse Video of "$yesterday  \
--category=Tech --keywords="Raspberry PI, Timelapse" /home/pi/$yesterday.avi) 
echo $video_link
youtube-upload --email=xxx --password=xxx--add-to-playlist http://gdata.youtube.com/feeds/api/playlists/PLbjjzh8UkLN2pQTZwXyQ4kP2bXb7Zwtmo $video_link


echo $(date)
/home/pi/Desktop/camera/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/Desktop/camera/video_maker.log /Public/pi_videos
find . -type f -name $DATE\* -exec rm {} \;
rm $three_days_ago.avi

The commands i want to implement it are

  • mencoder -nosound....
  • video_link=$(youtube-upload --email=xxx...

Manos

Posted 2014-10-17T09:20:13.623

Reputation: 145

Answers

1

You could wrap your two critical commands in a loop:

max_retries=3
for i in $(seq ${max_retries});do
    critical_command
    if [ $? == 0 ];then
        break
    fi
done

$? stores the return code of the last command. "0" means success, anything else means failure. Within the square brackets, I compare this return code to "0". If they are equal, the comparison yields "true", which means we enter the "if" block (if you use this, pay attention to the whitespaces around the brackets, they are important). There I issue a single "break", which tells the for loop that it should terminate immediatly, and not do any further iterations. In short: if the critical_command was successfull, carry on with the rest of the program.

This also works if the output of your command is caught in a variable, like with your video_link. For the uploader tool, it might be advisable to add a little pause bevor retrying, since the server might be busy:

critical_command
sleep 10 # wait for 10 seconds

I would wrap each command separately, even though its not the most elegant solution (you could create a wrapper function, which retries the command given as an argument - not sure though if this works in bash).

Isaac

Posted 2014-10-17T09:20:13.623

Reputation: 944

Can you explain me what does this means? if [ $? == 0 ];then break – Manos – 2014-10-17T15:43:44.397

I have edited my answer to explain this a little. – Isaac – 2014-10-20T06:29:07.073