A bash script to submit an AWS EMR job and poll until the cluster terminates #!/bin/bash
set -ue

# use AWS CLI to launch a cluster
create_cluster_response=$(aws emr create-cluster \
  --release-label emr-4.3.0 \
  --region us-east-1 \
  --etc) # many more arguments; see http://docs.aws.amazon.com/cli/latest/reference/emr/create-cluster.html

# use jq to parse and display result
cluster_id=$(echo "${create_cluster_response}" | jq -r '.ClusterId')
echo "ClusterId=${cluster_id}"
echo "AWS Management Console URL: https://console.aws.amazon.com/elasticmapreduce/home?region=us-east-1#cluster-details:${cluster_id}"

# block until the EMR cluster terminates
previous_state=''
while true; do
  json=$(aws emr describe-cluster --cluster-id ${cluster_id}) # see http://docs.aws.amazon.com/cli/latest/reference/emr/describe-cluster.html
  state=$(echo "${json}" | jq -r '.Cluster.Status.State')
  # echo state changes
  if [[ ${state} != ${previous_state} ]]; then
    now=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # format date as ISO 8601 UTC
    message=$(echo "${json}" | jq -r '.Cluster.Status.StateChangeReason.Message')
    echo "${now} ${state} ${message}"
    previous_state="${state}"
  fi
  # terminate this script when the cluster terminates
  if [[ ${state} == TERMINATED ]]; then
    echo "EMR JOB SUCCESS"
    exit 0
  fi
  if [[ ${state} == TERMINATED_WITH_ERRORS ]]; then
    echo "EMR JOB FAILURE"
    exit 1
  fi
  sleep 10
done


I am very new to programming bash and will appreciate any and all feedback.
you can use emr wait instead of describe-cluster

aws emr wait cluster-running --cluster-id &lt;value&gt; --region &lt;value&gt;


http://docs.aws.amazon.com/cli/latest/reference/emr/wait/cluster-running.html