8
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.

3
  • 1
    Shouldn't it be the other way around, while instead of until? You might never see that string if the command finishes fast, and you want to wait while it runs, not until it does. Commented Aug 22, 2018 at 21:05
  • i tried changing until with while but still the loop doesn't exit. i am unable to understand how to write this loop. Commented Aug 23, 2018 at 2:31
  • Check out github.com/nickjj/wait-until Commented Oct 3, 2023 at 3:31

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

Comments

6

This is a general function which I use:

function wait_for() {
    timeout=$1
    shift 1
    until [ $timeout -le 0 ] || ("$@" &> /dev/null); do
        echo waiting for "$@"
        sleep 1
        timeout=$(( timeout - 1 ))
    done
    if [ $timeout -le 0 ]; then
        return 1
    fi
}

You can have another function which checks the condition:

function is_still_running() {
    aws_status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
    if [ "${aws_status}" = *"InProgress"* ]; then
        return 0;
    else
        return 1;
    fi
}

then the usage would be:

wait_for 100 is_still_running

1 Comment

This is very nice, probably make a slight change so it can read like: wait_for_job aws_run or_timeout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.