4
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

这里循环永远不会退出,它会继续打印“自动化正在运行......”即使在自动化已经执行并且状态没有进行中我想要做的是等到状态为“进行中”,打印“自动化是正在运行……”在屏幕上。完成后,如果失败或成功,我想在屏幕上打印自动化状态。

4

2 回答 2

7

添加 if else until 帮助我摆脱了循环。

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
于 2018-08-24T20:32:36.363 回答
0

这是我使用的一般功能:

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
}

您可以使用另一个检查条件的函数:

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
}

那么用法是:

wait_for 100 is_still_running
于 2022-01-12T11:10:00.187 回答