run commands within loops and wait till running the next one

By user3311890

I am trying to run a bash script that has a function and inside each function there is a loop that runs three commands based on an array. here is a sample (makes more sense to see it)

#!/bin/bash

DIST="ubuntu"
declare -a POCKET=("release" "security" "updates")

#Xenial
echo "=======Xenial=======" >> /tmp/sync.log

xenial()
{
SERIES="xenial"

for pocket in "${POCKET[@]}"
do
        sync $pocket $SERIES $DIST >> /tmp/sync.log
done
}

xenial

if <status_of_function> != 0 then
email me the errors from the log file 

So the script runs fine, but it fails at the second instance of the array because the first instance takes a lot of time (maybe a few hours). Can I run the second instance of the loop array until the first one finished? I know I could do a sleep but that doesnt help as I dont know when is going to finish.

Also, can I get the status of the function? and if it’s not 0 (there was an error) then to send me an email with the log file. I plan to have similar functions for each ubuntu release

Any help will be greatly appreciated

Source:: http://askubuntu.com/questions/870741/run-commands-within-loops-and-wait-till-running-the-next-one


Leave a Reply