Wait until a condition is met in bash script

chandra picture chandra · Aug 22, 2018 · Viewed 9.6k times · Source
until [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) = *"InProgress"* ];
do
  echo "Automation is running......"
  sleep 1m
done
status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
if [ "$status" == "Success" ]; then
    echo "Automation $status"
elif [ "$status" == "Failed" -o "$status" == "Cancelled" -o "$status" == "Timed Out" ]; then
    echo "Automation $status"
fi

here the loop is never exiting, it keeps printing "Automation is running......" even after automation has been executed and status is not inprogress what i want to do is wait until status is " inprogress", print "Automation is running......" on screen. once its finished, i want to print the status of automation on screen if it failed or succeeded.

Answer

chandra picture chandra · Aug 24, 2018

adding an if else until helped me get out of the loop.

until [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) = *"InProgress"* ];
do
  echo "Automation is running......"
  sleep 10s
  if [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) != "InProgress" ]; then
     echo "Automation Finished"
     status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
     echo "Automation $status"
     if [$status != "Success"]; then
        exit 3
        echo "Automation $status"
     fi   
    break
  fi
done